This discussion is archived
4 Replies Latest reply: Dec 18, 2012 9:59 AM by Kayaman RSS

Garbage Collection Mysteries

870997 Newbie
Currently Being Moderated
I have an application that consumes huge amounts of heap at time, on the order of 100 MB. I added some code to call System.gc() when I dispose of the window that is responsible for making such huge allocations. My understanding is that System.gc() is supposed to do a full garbage collection, but when I am watching the heap in the Eclipse profiler it does not seem to reclaim very much. In almost ever case, when I click the garbage collect button in the profiler it seems to do a much better job of collecting a lot.

What is the difference between explicitly invoking the garbage collector from the Eclipse profiler and calling System.gc() in code when the window is disposed?

How can I get my code to automatically make the garbage collector work so well?

Cheers, Eric
  • 1. Re: Garbage Collection Mysteries
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    You can try calling gc() twice. With the Sun VM the second call is necessary to clean up everything.

    Might note however that calling gc() is unlikely to do anything to make your application better.
  • 2. Re: Garbage Collection Mysteries
    gimbal2 Guru
    Currently Being Moderated
    jschell wrote:
    Might note however that calling gc() is unlikely to do anything to make your application better.
    Ain't it the truth. In fact it may even make it worse. Sane applications should not need manual GC calls - the runtime does what it is supposed to do itself and if it doesn't you should investigate and fix the problem that is preventing it from doing what it is supposed to do. First making sure that there is in fact an issue of course, perhaps the app simply needs 100mb of heap space at some point in the lifetime of your application. It isn't exactly a HUGE amount of memory, especially in a Java or .NET VM environment.
  • 3. Re: Garbage Collection Mysteries
    980583 Newbie
    Currently Being Moderated
    Is there any particular reason you are looking for such precise control over memory?

    You might be better focusing on choosing an optimal GC strategy than attempting to execute System.gc within your own code. Quoting the javadoc "Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects" the key word being suggests, some JVM's will ignore you, others will immediately perform a full/major GC.
  • 4. Re: Garbage Collection Mysteries
    Kayaman Guru
    Currently Being Moderated
    Eric Kolotyluk wrote:
    I have an application that consumes huge amounts of heap at time, on the order of 100 MB.
    That is not a huge amount of heap. You are worrying about nothing.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points