What is Automatic Garbage Collection in Java – Explain – Java Tutorial
Problem Definition:
Write a short note on What is Automatic Garbage Collection in Java.
Solution:
Garbage collection is a method of automatic memory management.
It works as follows –
When an application needs some free space to allocate the nodes and if there is no free space available to allocate the memory for these objects then a system routine called garbage collector is called.
Video Tutorial Automatic Garbage Collection in Java
This routine then searches the system for the nodes that are no longer accessible from an external pointer. These nodes are then made available for reuse by adding them to the available pool. The system can then make use of these free available spaces for allocating the nodes.
Garbage collection is usually done in two phases – the marking phase and the collection phase.
In the marking phase, the garbage collector scans the entire system and marks all the nodes that can be accessed using an external pointer.
During the collection phase, the memory is scanned sequentially, and the unmarked nodes are made free.
Marking phase:
For marking each node, there is one field called the mark field. Each node that is accessible using an external pointer has the value TRUE in the marking field.
For example:
![Automatic Garbage Collection in Java](https://vtupulse.com/wp-content/uploads/2022/08/image-12.png)
Collection phase
During the collection phase, all the nodes that are marked FALSE are collected and made free.
This is called sweeping. There is another term used in regard to garbage collection called Thrashing.
Thrashing in Garbage Collection Java
Consider a scenario where the garbage collector is called for getting some free space and almost all the nodes are accessible by external pointers.
Now garbage collection routine executes and returns a small amount of space. Then again after some time system demands some free space.
Once again garbage collector gets invokes which returns a very small amount of free space.
This happens repeatedly and the garbage collection routine is executed almost all the time.
This process is called thrashing. Thrashing must be avoided for better system performance.
Advantages of garbage collection
1. The manual memory management done by the programmer (i.e. use of malloc and free) is time-consuming and error-prone. Hence automatic memory management is done.
2. Reusability of memory can be achieved with the help of garbage collection.
Disadvantages of garbage collection
1. The execution of the program is paused or stopped during the process of garbage collection.
2. Sometimes situations like thrashing may occur due to garbage collection.
The finalize() Method
Java has a facility for automatic garbage collection.
Hence even though we allocate the memory and then forget to deallocate it then the objects that are no longer is used get freed.
Inside the finalize() method you will specify those actions that must be performed before an object is destroyed.
The garbage collector runs periodically checking for objects that are no longer referenced by any running state or indirectly through other referenced objects.
Sometimes an object will need to perform some specific task before it is destroyed such as closing an open connection or releasing any resources held.
To handle such a situation, finalize() method is used.
finalize() method is called by garbage collection thread before collecting object.
It’s the last chance for any object to perform cleanup utility.
To add finalize() to a class simply define the finalize method.
The syntax to finalize() the code is –
protected void finalize()
{
finalization code
}
Note that finalize() method is called just before the garbage collection. It is not called when an object goes out-of-scope
Can the Garbage Collection be forced explicitly?
No, the Garbage Collection cannot be forced explicitly.
We may request JVM for garbage collection by calling System.gc() method.
But this does not guarantee that JVM will perform the garbage collection.
gc() method is used to call automatic garbage collector explicitly.
However, gc() method does not guarantee that JVM will perform the garbage collection.
It only requests the JVM for garbage collection.
This method is present in the System and Runtime class.
Sample Program:
public class Test { public static void main(String[] args) { Test t = new Test(); t=null; System.gc(); } public void finalize() { System.out.println("Garbage Collected"); } }
Output :
Garbage Collected
Summary:
In this article, we understood, What is Automatic Garbage Collection in Java Advantages and Disadvantages of garbage collection, Finalize Method. Can the Garbage Collection be forced explicitly? – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.