Reputation: 12042
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
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
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
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
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
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
Reputation: 143304
while(statsIterator.hasNext()){
i++;
PerfStatBean perfBean = statsIterator.next();
statsIterator.remove();
Upvotes: 1
Reputation: 14974
ListIterator<PerfStatBean> statsIterator = statFilterResults.iterator();
while(statsIterator.hasNext()){
i++;
PerfStatBean perfBean = statsIterator.next();
...
statsIterator.remove();
}
Upvotes: 1