previous |
start |
next
Deallocation of Java Objects - The Garbage Collector
- Objects are created with the
new
operator.
new
returns a reference to the new object.
In Java there is no explicit memory (object) deallocation user operator.
When an object looses all references, it becomes eligible for deallocation
by the JVM garbage collector
. (quick intro
here).
- Before an object is actually destroyed, the GC invokes its
finalize()
method,
defined in class Object
. This method may implement custom
code for deallocating
resources (e.g. file descriptors, sockets,...), although it is
not advisable - better use
an explicit method for cleanup.
- Example in
LifeSample.java.
- Invoke the JVM garbage collector with the
System.gc();
method call.
Invoke GC before long sequences of memory intensive code.
previous |
start |
next