Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason everyone always says to avoid <code>System.gc()</code> is that it is a <em>pretty good indicator of fundamentally broken code</em>. Any code that depends on it for correctness is certainly broken; any that rely on it for performance are most likely broken.</p> <p>You don't know what sort of garbage collector you are running under. There are certainly some that do not <em>"stop the world"</em> as you assert, but some JVMs aren't that smart or for various reasons (perhaps they are on a phone?) don't do it. You don't know what it's going to do.</p> <p>Also, it's not guaranteed to do anything. The JVM may just entirely ignore your request.</p> <p>The combination of "you don't know what it will do," "you don't know if it will even help," and "you shouldn't need to call it anyway" are why people are so forceful in saying that generally you shouldn't call it. I think it's a case of "if you need to ask whether you should be using this, you shouldn't"</p> <hr> <p><strong>EDIT</strong> to address a few concerns from the other thread:</p> <p>After reading the thread you linked, there's a few more things I'd like to point out. First, someone suggested that calling <code>gc()</code> may return memory to the system. That's certainly not necessarily true - the Java heap itself grows independently of Java allocations. </p> <p>As in, the JVM will hold memory (many tens of megabytes) and grow the heap as necessary. It doesn't necessarily return that memory to the system even when you free Java objects; it is perfectly free to hold on to the allocated memory to use for future Java allocations.</p> <p>To show that it's possible that <code>System.gc()</code> does nothing, view:</p> <p><a href="http://bugs.sun.com/view_bug.do?bug_id=6668279" rel="noreferrer">http://bugs.sun.com/view_bug.do?bug_id=6668279</a></p> <p>and in particular that there's a <a href="http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp" rel="noreferrer">-XX:DisableExplicitGC</a> VM option.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload