In C / C++ it is the responsibility of the programmer to destroy the objects she has created, once they are no longer needed. If she does not destroy those unneeded objects, she may face memory allocation issues. At a certain point of time in the program, when the user creates a new object, if sufficient memory is not available, an error would occur. The program would end abnormally. In Java this problem is taken care by Java Garbage Collection.
While coding in Java, the programmer need not care about all those objects which are no longer needed. The garbage collector in java destroys all these objects creating memory space for new objects. Garbage collector is a thread which always runs in the background. The main objective of the garbage collector is to free the heap memory by destroying the unreachable objects. Unreachable objects are those which don’t have a reference to access it. Java Garbage Collection helps in memory management with ease.
Definition-Java Garbage Collection
Java garbage collection is the process by which java programs perform automatic memory management with the help of garbage collector. When we compile any java program a byte code is generated which runs on a JVM. When it runs on the JVM, objects are created on a heap, which is a dedicated memory portion, allotted to that program.
Once an object is created, it remains in the allocated memory till there are references for the use of these objects. As long as an object is referenced, JVM considers it to be alive. As the program progresses, some objects are no longer needed. The garbage collector finds these unneeded objects and deletes / de-allocates these objects to free up some memory space.
Programs that do not de-allocate memory can eventually crash if no memory is left in the system to allocate. These programs are said to have memory leaks.
How Java Garbage Collection actually works?
To determine which objects are no longer in use, the JVM runs an algorithm called mark-and-sweep algorithm. It has two steps. These are:
- The algorithm traverses all object references and marks every object found as alive.
- Rest of the memory, that is not marked, is reclaimed and it is marked as free.
Java provides automatic garbage collection, but we can also accomplish these tasks using some functions. These are:
- totalMemory() – This function returns the total number of bytes of the memory available to the program.
- freeMemory() – This function returns the approximate number of bytes of the free memory available to the java run-time system. This will give an idea to the programmer about how many more objects (of a certain type) can be instantiated.
- gc() – This function initiates garbage collection.
When an object needs to perform some action when it is destroyed, we make use of finalize() function. The garbage collector calls finalize () method just before destroying the object. Hence, by using finalization, we can define various actions that will occur when the object is just about to be reclaimed by the garbage collector. Once finalize() method is completed, the garbage collector destroys the object.
To add finalize() method to a class, we define it as follows:
protected void finalize()
// finalization code comes here
The protected specifier prevents access to finalize() by code which is defined outside the class. Inside the method we can specify the actions that we want it to perform before the object is destroyed.
You should note that:
- finalize() method is called by the Garbage Collector and not JVM.
- finalize() method is invoked only once for any given object that too just before the object is destroyed.