Home > Back-end >  How to clear an ArrayList object java
How to clear an ArrayList object java

Time:12-28

I am new to java so this has come across in a beginner java problem.

I am aware that it is bad practice to leave an object open once it is no longer in use, however I cannot find an example of this for an ArrayList: the tutorials I have looked at leave it open and I can't find anything other than clearing it to remove all items in it.

I am looking for the equivalent to this:

import java.util.Scanner;

Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();
sc.close();

but for the ArrayList object:

import java.util.ArrayList;

ArrayList<String> countries = new ArrayList<String>();
countries.add("Spain");
countries.add("China");
System.out.println(countries);
//And then something like
countries.close();

Is it just ArrayList<Object>.clear()? but that doesn't remove the object? Thank you

CodePudding user response:

Using .clear() will remove all items from the list, but it looks like you want to tell the garbage collector to delete the object. As far I know, there is no way to do this. However, there is also no need to worry about deleting objects. If your code stops using them, the garbage collector will simply delete them from memory automatically.

CodePudding user response:

I am aware that it is bad practice to leave an object open once it is no longer in use, however I cannot find an example of this for an ArrayList ...

It is NOT in general (a) "bad practice" to leave objects "open".

Failing to close objects can cause problems if they have associated non-heap resources. The most common examples are:

  • Objects that have an associated file descriptor; e.g. Socket, FileInputStream and FileOuputStream.
  • Objects that have an associated "connection" to an external service; e.g. JDBC Connection, Statement and ResultSet objects.
  • Objects that have associated off-heap storage; e.g. BufferedImage.

However, most Java objects don't have this problem, and you don't need to "close" them. Indeed, most objects don't have a close() or dispose() method.

ArrayList is typical. It is not necessary to "close" or "clean" an ArrayList. Just leave it for the garbage collector to clean up.

In exception circumstances, it might be advisable to clean a List object; e.g. so that you can reuse it. If you need to do that, call the clear() method.

However, that is generally a bad idea. In the case of ArrayList, calling clear() can lead to a couple of second order performance issues:

  1. Clearing a list entails touching all of the list cells. That consumes CPU time.

  2. The clear() method does not resize the ArrayList backing array. So a recycled list that becomes large will continue to have a large backing array ... for ever ... if you keep recycling it.

  3. With most Java GC's, a Java object that survives a number of GC cycles will be promoted to the "old" generation. When that happens, the associate GC costs increase. For example, when you assign a reference to a new object to an old object (say our list) that causes the new object (and possibly nearby objects) to NOT be collected in the next new space collection; i.e. it extends its lifetime.

In short, clear() can be bad for performance.


Beware of good / best / bad practice advice. A lot of it is "snake oil". And even at the best of times, most such advice doesn't apply universally. People who like to reply on so-called "best practice" advice (or offer it!) should read this:

CodePudding user response:

In General, ArrayList can be left open and if you want to remove all the objects from the List you can use .clear function. Generally streams implement the closable interface and needed to be closed. Reason we try to write the efficient code by reducing the space complexity because the lists remain in the code as it is.

  • Related