Javatpoint Logo
Javatpoint Logo

How to prevent objects of a class from Garbage Collection in Java

We know that when an object is unreferenced, that object is automatically gobbled up by the garbage collector. In this tutorial, we will come to know how to avoid garbage collection of objects in Java. However, before that, let us take an example of garbage collection.

FileName: ABC.java

Output:

Inside the method foo.

Explanation: The reference variable scope is limited to the method foo(). Therefore, when the control goes out of the method, the reference r dies, making the object created in the method foo() unreferenced and hence, making the object ready to be gobbled up by the garbage collector.

There are several ways to ensure that objects are not deleted in Java. They are mentioned below.

By Increasing Heap Memory

In Java, memory is allocated to the object in a partition known as 'heap'. Also, it is a known fact that there is a limitation of heap memory. Therefore there is always a need to free up the memory so that it can be allocated to new objects. However, one may increase the heap memory, which will help to make the objects undeletable to some extent.

With The Help of Singleton Class

In the case of the Singleton class, only one object is created, which is referenced by a static variable. Also, we know that the memory of the static variables is allocated in the class area ( a type of memory) and not stack. Hence, the life of the static variables lasts till the lifespan of the program. The following program illustrates how one can do it.

FileName: Singletn.java

Output:

Inside the display method.

The above example illustrates that the reference of the object is held by a static variable that lasts until the program has ended. Therefore, we can also say that the object is not unreferenced till the lifespan of the program, making it undeletable.

With The Help of public void finalise()

The finalize() method is a call-back method. It is a method that is not called by a user. Instead of that, it has been invoked by the JVM. The finalize() method is the last method that has been executed on an object. A subclass can override the method finalize() in order to dispose of resources of the system or to do the other cleanup. So, what one can do is store the reference of the object that is going to be deleted. An illustration of it is shown below.

FileName: DemonstrateFinalize.java

Output:

The reference of the object is saved. The object will not be gobbled by the garbage collector.

In the above program, only one object of the class is created, and that too in the method foo(). So, when the execution of the method ends, the reference obj is destroyed, making the object unreferenced. Therefore, when the garbage collector is run to gobble the unreferenced object, the finalize() method executes, and in that method, the unreferenced object gets the reference df, and hence, the garbage collector can not gobble the object.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA