Tam
Tam

Reputation: 12042

nullify object that is part of ArrayList java

I have an array list:

private ArrayList<PerfStatBean> statFilterResults;

I want to iterate through it like:

Iterator<PerfStatBean> statsIterator = statFilterResults.iterator();
while(statsIterator.hasNext()){
  i++;
  PerfStatBean perfBean = statsIterator.next();
  .........

I would like to delete the bean from statFilterResults after I run through it inside the while loop to free up memory. I believe if I do something like

 perfBean = null;

it won't do the job as the perfBean reference will be null but the object will still be in memory.

Any ideas?

Thanks,

Tam

Upvotes: 3

Views: 1788

Answers (8)

OscarRyz
OscarRyz

Reputation: 199274

...I would like to delete the bean from statFilterResults after I run through it inside the while loop to free up memory.

If there is another reference of the object outside the arraylist, setting it to null won't make any difference, the memory for that object is used only once, and in java you only see references ( or reference values )

So if you have something like:

this.bean = new PerfStatBean();

....

arrayList.add( this.bean );

Removing it from the list won't make any difference because the original reference will still exists and won't get gc'ed.

If you don't have other references outside the collection ie.

 arrayList.add( new PerfStatBean() );

Then this should be enough:

while(....){
    .... 
}

arrayList.clear();

Because that will remove the references to the object and that would make them eligible for gc.

Furthermore: if you want to "help" reduce the number of references an object has, invoking:

while(....){
    .... 
}

arrayList.clear();

Should be enough.

Upvotes: 0

nos
nos

Reputation: 229234

Why not simply clear the list after you have iterated it ?

Iterator<PerfStatBean> statsIterator = statFilterResults.iterator();
while(statsIterator.hasNext()){
  i++;
  PerfStatBean perfBean = statsIterator.next();
  .........
}

statFilterResults.clear();

Upvotes: 2

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

This probably isn't a great optimisation. If the memory requirements are large, then you probably don't want to store them all in a list in the first place - process the objects as they are created.

However, taking the question literally, ListIterator.set is the method for you.

for (
    ListIterator<PerfStatBean> iter = statFilterResults.listIterator();
    iter.hasNext()
) {
    ++i;
    PerfStatBean perfBean = iter.next();
    iter.set(null);
    ...
}

Upvotes: 3

Torandi
Torandi

Reputation: 1625

In java you don't really need to bother about deleting objects. If there are no references to an object the next time the Garbage Collector runs, it will be deleted (the GC is run when there arefree cpu cycles). If you wish to make sure the object is deleted directly, use Runtime.getRuntime().gc()

Upvotes: -1

Carl Manaster
Carl Manaster

Reputation: 40356

Create a set before the loop, then add any items to be deleted to that set. After the loop, call

statFilterResults.removeAll(beansToDelete);

Also, and by the way, I find it to be generally easier - more readable, more maintainable, etc. - to use a foreach loop instead of an iterator. In your case, that would look something like:

for (PerfStatBean bean : statFilterResults) {
    ...
}

Upvotes: 1

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143304

while(statsIterator.hasNext()){
  i++;
  PerfStatBean perfBean = statsIterator.next();
  statsIterator.remove();

Upvotes: 1

ykaganovich
ykaganovich

Reputation: 14974

ListIterator<PerfStatBean> statsIterator = statFilterResults.iterator();
while(statsIterator.hasNext()){
  i++;
  PerfStatBean perfBean = statsIterator.next();
  ...
  statsIterator.remove();
}

Upvotes: 1

toolkit
toolkit

Reputation: 50287

iterator.remove()?

Upvotes: 2

Related Questions