|
The Java objects are created on the heap. Since the memory on the heap is managed by Java, you never explicitly free the memory. To manage the heap-memory, Java runtime system keeps a track of memory allocation. Therefore, it can somehow (we will see later in GC algorithms) determine which memory is in use and which is not at any given time. Java considers an object as garbage when it can no longer be reached from any reference in the running program. Memory allocated to such object is unusable and must be returned back to the heap. Let us see how an object becomes unreachable with an example. Assume that there is a Car class. Now we will create two objects of this class and make one of them garbage-
1. Car beetle = new Car(); // first Car object created
2. Car bug = new Car(); // second Car object created
3. beetle = bug; // first Car object becomes unreachable
Figure 6.1 shows the memory model after the first two lines are executed. Objects are always allocated memory on heap at the time of their creation. The figure shows two Car objects and two reference variables beetle and bug, are pointing to them respectively.

Figure 6.1 Memory model when two Car objects are created
After the third line in the code (beetle = bug;) is executed, the reference beetle is modified. It now pointes to the Car object created on line 2. Hence, the first Car object (initially pointed to by beetle ) becomes unreachable because now no reference is pointing it. This unreachable object is of no use for the current program as it cannot access it. Such unreachable object is considered to be a garbage. The memory allocated to it must be released and returned to heap as a free memory. That is exactly what the garbage collector does. It is a part of JVM responsible for releasing the memory allocated to garbage objects. Whenever it runs, it searches for all the garbage objects and recycles the memory allocated to them. Figure 6.2 shows how the reference beetle points to the second Car object making the first Car object unreachable.
![]() |
Figure 6.2 Memory model after the first Car becomes unreachable in the currently running code.
Figure shows the unreachable Car object with a garbage bag and garbage collector with a truck metaphor. The step, which collects the garbage object, is shown with a question mark (?). It indicates that we do not know exactly when the garbage collector runs and picks the garbage object. But as you can see, the garbage objects are eventually collected and memory is recycled back to the heap.
|
|
The garbage object is unreachable to any object reference in the running program, even to the threads (Chapter 12), since Java threads are also objects. In the above example we said that the first Car object was no longer reachable after line 3 because we assumed that only one thread was accessing this piece of code. However, a Java program may have more that one threads running at the same time. In fact, the garbage collector itself runs as a (low-priority) thread in parallel with your program. So, when multiple threads are running, the object is considered as garbage when none of them can reach it. While answering the garbage-collection question, you can safely assume that only one thread is executing the given code unless it is specified otherwise. |
Thus, you might be able to predict which object becomes garbage (becomes eligible for garbage-collection) in any Java code (by only looking at the code). However, you cannot be certain when actually the garbage collector picks it for recycling.