Does sublist(from,to).clear() allow garbage collection of the cleared part of an ArrayList?

Clear" is relocating objects in the underlying native array (an Object), but it doesn't resize the array. If you want reduce the array size after removing some items in the ArrayList, use trimToSize() method.

Clear" is relocating objects in the underlying native array (an Object), but it doesn't resize the array. If you want reduce the array size after removing some items in the ArrayList, use trimToSize() method. Unused element references of the array are set to null, so the elements could be garbage collected.

1 +1: You only need to do this is you are worried about the unused references. (Almost never an issue) The objects will get available to be cleaned up regardless. – Peter Lawrey Sep 7 '11 at 9:04 Actually, trimToSize() is what I was looking for.

I reformulated the question. I am actually more interested in the real internal allocated memory of the ArrayList than in the GC of the items it contains. – ptikobj Sep 7 '11 at 9:50.

When you clear a sublist, its the same as removing those entries, so all of them could be GCed (less they are referenced somewhere else) The whole point of managed memory objects is that you don't need to worry about how and when they are cleaned up. I wouldn't worry about it unless you know you have a problem. In which case I would use a memory profiler to determine why objects are being retained when you think they shouldn't.

Yes, if you get a sublist and clear it, you'll remove all the elements in the sublist from the original list. In other words, if the list is the only one storing references to the objects, the objects you remove are eligible for garbage collection. Basic demo: List strings = new ArrayList(); strings.

Add("one"); strings. Add("two"); strings. Add("three"); strings.

Add("four"); System.out. Println(strings); // prints one, two, three, four strings. SubList(1, 3).clear(); System.out.

Println(strings); // prints one, four.

That returned list is a VIEW of the original. If you modify one, changes may be visible on the other.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions