16

I'm running my application under a profiler and the memory usage is much higher than I expect, where objects are still in existence after they are no longer needed. Most of them are in lists where the list object has gone out of context.

Does it take the garbage collector longer to free up objects that are sitting in a list, even if the list itself is no longer referenced? If so, will they free up sooner if I call clear() on the list before it goes out of context?

thanks - dave

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • 4
    How you used an object before it is no longer referenced makes no difference to how long it takes to clean up. – Peter Lawrey Mar 25 '14 at 12:59
  • Your objects may have been promoted to the older generation in the GC which causes it to take longer and more expensive to GCed. You may need to tune the GC to suit your application so that most of your objects remain in Eden space. – anonymous Mar 25 '14 at 13:21

4 Answers4

9

In terms of the garbage collection mechanics, you are asking whether GC roots have any priority over all other, transitively reachable objects in being detected as unreachable. Given the way the mark-sweep algorithms work, there is no difference between these two. All such garbage objects will be marked in the same pass and the internal reachability of an object from within an unreachable object has no impact on this.

Finding a lot of unreachable objects still not being reclaimed is nothing unusual and it is actually to the benefit of overall application performance: only when memory allocation fails will the GC run. If you are worried about the size of your heap, then simply reduce the maximum heap size. This will cause more frequent garbage collection.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
3

If we take an ArrayList as an example, the clear() method does this:

public void clear() {
    modCount++;

    // Let gc do its work
    for (int i = 0; i < size; i++)
        elementData[i] = null;

    size = 0;
}

Basically, if a List and elements of the List are not referenced anywhere else in the code there is really no need to call clear() (or at least you do not gain anything by doing so). You also do not need to assign null to the List variable because it will be garbage collected at some point after it falls out of scope (again, if the list itself is not referenced anywhere else).

Check out how "mark and sweep" garbage collection algorithm works: http://wiki.c2.com/?MarkAndSweep

darijan
  • 9,725
  • 25
  • 38
1

Yes. clear will remove references to objects in the list, at least the ArrayList implementation of list will do so. However if your whole List object is going out of scope anyways, you don't gain anything.

RokL
  • 2,663
  • 3
  • 22
  • 26
1

I know u got your answer but wanted to clarify... if your old gen has a size of 1 GB but you only store 1 MB inside it, then there is absolutely no need for the GC to run. As a matter of fact you do not want it to run at this time, because if it would then you would have stop-the-world events triggered by the mark-sweep algorithm (by mark and re-mark phases).

GC is triggered only when there is pressure on the memory.

Eugene
  • 117,005
  • 15
  • 201
  • 306