Java Reference
In-Depth Information
// Clear the referent's reference
r.clear();
// Wait for second phantom reference to be queued
r = q.remove();
// Clear the referent's reference
r.clear();
// Wait for third phantom reference to be queued
r = q.remove();
// Clear the referent's reference
r.clear();
System.out.println("All three objects have been " +
"queued and cleared.");
/* Typically, you will release the network connection or
any resources shared by three objects here.
*/
// Exit the application
System.exit(1);
}
catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
} );
// Start the thread, which will wait for three phantom
// references to be queued
t.start();
}
}
finalize() called for id:103
finalize() called for id:102
finalize() called for id:101
All three objects have been queued and cleared.
Summary
The process of reclaiming the memory of dead objects is known as garbage collection. Garbage collection in Java is
automatic. The Java runtime runs garbage collection in a low priority background thread. The JVM does its best to
free up memory of dead objects before throwing an OutOfMemoryError . You can pass a hint, although not needed in
an application, to the JVM by calling Runtime.getRuntime().gc() . You can also use the convenience method
System.gc() to pass the hint to the JVM. The JVM is free to ignore the hint.
 
Search WWH ::




Custom Search