Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    singulars
    1. This table or related slice is empty.
    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.
    1. COIt's not true that objects cease to be reachable once they are out of scope. Such objects have *method* scope from the point of view of reachability. So `public void run() { {Object o = new Object(); System.out.println(o);} /* o can no longer be used but it still exists! So the following is a memory leak! */ while(true) { try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException e) { } } }` never cleans up o without an `o = null` before exiting the inner block. Why this is so makes sense if you think about it in terms of the virtual machine and Java byte code instructions.
      singulars
    2. COJeremy. There is no memory leak in your example above, and nobody needs to store a null anywhere for that allocated object to go away. When you reach the while loop (in your example) o is out of scope. The object it referred to is no longer reachable and there is no way to access it or to to re-establish reachability to it. It is dead and gone, and the next GC will happily reclaim the space it had occupied as needed to satisfy some future allocations.
      singulars
    3. CO@GilTene, objects are reachable until the stack frame is exited, i.e. when the method exits. Extra braces to create inner scope in methods are syntactic and do not affect the stack frame. Thus the following two tests pass `@Test public void testNoGC() { WeakReference<Object> ref; { Object o = new Object(); ref = new WeakReference<Object>(o); } System.gc(); assertNotNull(ref.get()); } @Test public void testGC() { WeakReference<Object> ref; { Object o = new Object(); ref = new WeakReference<Object>(o); o = null; } System.gc(); assertNull(ref.get()); }`
      singulars
 

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