Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The article states that:</p> <blockquote> <p>... an efficient implementation of the JVM is unlikely to zero the reference when it goes out of scope</p> </blockquote> <p>I think this happens because of situations like this:</p> <pre><code>public void doSomething() { for(int i = 0; i &lt; 10 ; i++) { String s = new String("boo"); System.out.println(s); } } </code></pre> <p>Here, the same reference is used by the "efficient JVM" in each declaration of String s, but there will be 10 new Strings in the heap if the GC doesn't kick in.</p> <p>In the article example I think that the reference to foo keeps in the stack because the "efficient JVM" <em>thinks</em> that is very likely that another foo object will be created and, if so, it will use the same reference. Thoughts???</p> <pre><code>public void run() { try { Object foo = new Object(); foo.doSomething(); } catch (Exception e) { // whatever } while (true) { // do stuff } // loop forever } </code></pre> <p>I've also performed the next test with profiling:</p> <pre><code>public class A { public static void main(String[] args) { A a = new A(); a.test4(); } public void test1() { for(int i = 0; i &lt; 10 ; i++) { B b = new B(); System.out.println(b.toString()); } System.out.println("b is collected"); } public void test2() { try { B b = new B(); System.out.println(b.toString()); } catch (Exception e) { } System.out.println("b is invisible"); } public void test3() { if (true) { B b = new B(); System.out.println(b.toString()); } System.out.println("b is invisible"); } public void test4() { int i = 0; while (i &lt; 10) { B b = new B(); System.out.println(b.toString()); i++; } System.out.println("b is collected"); } public A() { } class B { public B() { } @Override public String toString() { return "I'm B."; } } } </code></pre> <p>and come to the conclusions:</p> <p>teste1 -> b is collected</p> <p>teste2 -> b is invisible</p> <p>teste3 -> b is invisible</p> <p>teste4 -> b is collected</p> <p>... so I think that, in loops, the JVM doesn't create invisible variables when the loop ends because it's unlikely they will be declared again outside the loop.</p> <p>Any Thoughts??</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