×
☰ See All Chapters

Java Garbage Collection

Since objects are dynamically allocated by using the new operator, you might be wondering how such objects are destroyed and their memory released for later reallocation. In some languages, such as C++, dynamically allocated objects must be manually released by use of a delete operator. If you forget to delete an object in C++ and you lose any references to it, the memory can never be freed and you have created what is referred to as a memory leak, which can wreak havoc on your programs, especially programs that use large amounts of memory or run for long periods.

In Java, there is no keyword or operator that you can use to remove an object from memory. Java was designed to avoid the problems of memory leaks that arise in other languages. A JVM has a low-level thread known as the garbage collector that is constantly running in the background, looking for objects in your Java program that are no longer being used and freeing their memory.

  1. An object is marked for garbage collection when it is no longer reachable in your program. 

  2. Garbage collector will not keep count of how many references there are to an object and then frees the object when the reference count is zero. Garbage collector will free up memory only when object is no longer reachable in your program. 

  3. We can make objects unreachable by assign the references to null, assign them to point to some other object, or make the references go out of scope. But we should make sure that object we want to be garbage collected is no longer referring to the object 

  4. We can invoke Garbage collector using the method System.gc(), which causes the garbage collector to “expend effort towards recycling unused objects. The gc() method is very much JMV dependent, so its behavior is hard to predict. However, it is your only mechanism for communicating with the garbage collector. 

Disadvantages of Garbage Collection: Programmers cannot explicitly free memory. Memory will be freed in a Java program only when the garbage collector concludes that the memory is no longer being used.

The following GCDemo program instantiates three Employee objects. Study the program carefully and try to determine at which point in the program that each Employee will be eligible for garbage collection.

public class GCDemo {

        public static void main(String[] args) {

                Employee e1, e2, e3;

                e1 = new Employee(); // Employee #1

                e2 = new Employee(); // Employee #2

                e3 = new Employee(); // Employee #3

                e2 = e1;

                e3 = null;

                e1 = null;

        }

}

The GCDemo program creates three references and assigns each to a new Employee object. The new keyword is used three times, so there are three objects in the program. The result of assigning e1 to e2 is that employee #2 no longer has a reference to it and can be garbage collected at any point   after   the    statement

e2 = e1. Note that now employee #1 has two references pointing to it.

 Assigning e3 to null makes employee #3 to be immediately eligible for garbage collection because the object can no longer be reached. In fact, if we decide that the object should be retrieved for some reason, it is too late. There is no way to relocate the object after all references to it have been lost.

 Assigning e1 to null does not cause employee #1 to be garbage collected because e2 still refers to the object. The reference e2 goes out of scope at the end of main(), so the employee #1 object can be garbage collected immediately after main() is done executing.

 


All Chapters
Author