Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree with @Joachim. </p> <h2>The below suggestion was a myth</h2> <p><em>In addition, it is recommended (in good coding convention) that <strong>do not declare any object inside the loop</strong>. Instead, declare it just before the loop start and use the same reference for initialization purpose. This will ask your code to use the same reference for each iterations and cause less burden on memory release thread (i.e. Garbage collection).</em> </p> <p><strong>The Truth</strong><br> I have <strong>edited</strong> this because I feel that there may be many people who (like me before today) still believe that declaring an object inside loop could harm the memory management; which is wrong.<br> To demonstrate this, I have used the same code posted on <a href="https://stackoverflow.com/questions/25552/using-java-to-get-os-level-system-information">stackOverflow for this</a>.<br> Following is my code snippet</p> <pre><code>package navsoft.advskill.test; import java.util.ArrayList; public class MemoryTest { /** * @param args */ public static void main(String[] args) { /* Total number of processors or cores available to the JVM */ System.out.println("Available processors (cores): " + Runtime.getRuntime().availableProcessors()); /* * Total amount of free memory available to the JVM */ long freeMemory = Runtime.getRuntime().freeMemory(); System.out.println("Free memory (bytes): " + freeMemory); /* * This will return Long.MAX_VALUE if there is no preset limit */ long maxMemory = Runtime.getRuntime().maxMemory(); /* * Maximum amount of memory the JVM will attempt to use */ System.out.println("Maximum memory (bytes): " + (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory)); /* * Total memory currently in use by the JVM */ System.out.println("Total memory (bytes): " + Runtime.getRuntime().totalMemory()); final int LIMIT_COUNTER = 1000000; //System.out.println("Testing Only for print..."); System.out.println("Testing for Collection inside Loop..."); //System.out.println("Testing for Collection outside Loop..."); //ArrayList&lt;String&gt; arr; for (int i = 0; i &lt; LIMIT_COUNTER; ++i) { //arr = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; arr = new ArrayList&lt;String&gt;(); System.out.println("" + i + ". Occupied(OldFree - currentFree): "+ (freeMemory - Runtime.getRuntime().freeMemory())); } System.out.println("Occupied At the End: "+ (freeMemory - Runtime.getRuntime().freeMemory())); System.out.println("End of Test"); } } </code></pre> <p>The result from the output is clearly shows that there is no difference in occupying/freeing the memory if you either declare the object inside or outside the loop. So it is recommended to have the declaration to as small scope as it can.<br> I pay my thanks to all the experts on StackOverflow (specially @Miserable Variable) for guiding me on this. </p> <p>Hope this would clear your doubts too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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