Java itself does memory management. You do not need to
allocate memory at the time of object creation; also you do not need to
free memory explicitly
Object is created either on the heap or on a stack
Memory heap: Objects created with new keyword are placed in heaps. This
memory remains allocated throughout the life cycle of the object. When
the object is no more referred, the memory becomes eligible for garbage
Stack: During method calls, objects are created for method
arguments and method variables. These objects are created on stack.
Such objects are eligible for garbage-collection when they go out of
Garbage Collection is a low-priority thread in java
Garbage Collection cannot be forced explicitly. JVM may do garbage collection if it is running short of memory.
The call System.gc() does NOT force the garbage collection but only
suggests that the JVM may make an effort to do garbage collection.
Garbage Collection is hardwired in Java runtime system.
Java runtime system keeps the track of memory allocated. Therefore, it
is able to determine if memory is still usable by any live thread. If
not, then garbage collection thread will eventually release the memory
back to the heap.
Garbage Collection usually adopts an algorithm, which
gives a fair balance between responsiveness (how quickly
garbage-collection thread yields?) and speed of memory recovery
(important for memory-intensive operations). Responsiveness is
especially important in real time systems.
An object is eligible for garbage collection when no object refers to it.
An object also becomes eligible when its reference is set to null.
(Actually all references to the object should be null for it to be
The objects referred by method variables or local
variables are eligible for garbage collection when the method or their
container block exits