Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is it bad practice to call System.gc()?
    primarykey
    data
    text
    <p>After <a href="https://stackoverflow.com/a/2406360/3650944">answering</a> a question about how to <a href="https://stackoverflow.com/questions/2406349/forcing-deallocation-of-large-cache-object-in-java/">force-free objects in Java</a> (the guy was clearing a 1.5GB HashMap) with <code>System.gc()</code>, I was told it's bad practice to call <code>System.gc()</code> manually, but the comments were not entirely convincing. In addition, no one seemed to dare to upvote, nor downvote my answer.</p> <p>I was told there that it's bad practice, but then I was also told that garbage collector runs don't systematically stop the world anymore, and that it could also effectively be used by the JVM only as a hint, so I'm kind of at loss.</p> <p>I do understand that the JVM usually knows better than you when it needs to reclaim memory. I also understand that worrying about a few kilobytes of data is silly. I also understand that even megabytes of data isn't what it was a few years back. But still, 1.5 gigabytes? And you <em>know</em> there's like 1.5 GB of data hanging around in memory; it's not like it's a shot in the dark. Is <code>System.gc()</code> systematically bad, or is there some point at which it becomes okay?</p> <p>So the question is actually double:</p> <ul> <li>Why is or isn't it bad practice to call <code>System.gc()</code>? Is it really merely a hint to the JVM under certain implementations, or is it always a full collection cycle? Are there really garbage collector implementations that can do their work without stopping the world? Please shed some light over the various assertions people have made in the comments to my <a href="https://stackoverflow.com/a/2406360/3650944">answer</a>.</li> <li>Where's the threshold? Is it <em>never</em> a good idea to call <code>System.gc()</code>, or are there times when it's acceptable? If so, what are those times?</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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