Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The joy of using Java, is that memory is managed for you behind the scenes so that, unlike C or C++, you don't have to worry about deconstructing or disposing of objects. When an object is no longer "usable" (as defined by falling out of scope and being unreachable from another active object) then the garbage collector quietly reclaims the space it was occupying.</p> <p>In Java, you cannot control when objects are garbage collected nor should you try.</p> <p>Code that depends on a deterministic garbage collection of unused objects will invariably be fragile and difficult to maintain in Java. in part this is because different JVM implementations will garbage collect at different times. <code>System.gc();</code> is, at best, a suggestion to the JVM that it do garbage collection, but is no guarantee when (or even if) it will happen.</p> <p>The best thing you can do is design your program so that reference variables have the absolute shortest possible lifespan. Your code is at risk of memory leaks any time a long-lived object retains a reference to a short-lived object (listeners are an example) or when you create data structures that "manage memory" themselves (eg. your own queue or stack implementation).</p> <pre><code>ArrayList&lt;Cannon&gt; cannonList = Cannon.getCannons(); for (int i = 0; i &lt; cannonList.size(); i++) { Cannon c = (Cannon) cannonList.get(i); c = null; } for (int i = 0; i &lt; cannonList.size(); i++) { cannonList.remove(i); } System.gc(); </code></pre> <p>In this snippet, there are several issues:</p> <p>When you use a data structure from the Java Collections API, you should use the interface as the type, not the concrete class. This is by convention, but doing so will keep your code more flexible. Instead of...</p> <pre><code>ArrayList&lt;Cannon&gt; cannonList = Cannon.getCannons(); </code></pre> <p>write this instead (valid for any class instances that implement the List interface):</p> <pre><code>List&lt;Cannon&gt; cannonList = Cannon.getCannons(); </code></pre> <p>When possible, you should use the enhanced for-each loop introduced with Java SE 5. It is less error prone. Your for loops should look like this:</p> <pre><code>for (Cannon c : cannonList) { c = null; // this for loop actually accomplishes no useful work since // the reference is null'd as soon as it gets a reference to // a Cannon object. The reference in your ArrayList is unaffected // by this assignment } cannonList.clear(); // more concise than removing each element from the list. </code></pre> <p>TL;DR: Garbage collection occurs when it happens. It is non-deterministic. You are never guaranteed when or even if it will happen. Design your Java programs to make your objects eligible for garbage collection at the earliest possible time ... and then don't worry at all about what happens.</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