Java Garbage Collection
Java Garbage Collection
In Java, garbage collection is a process by which the Java Virtual Machine (JVM) automatically manages memory. In some languages, such as C++, dynamically allocated objects must be manually released by using the delete operator.
What is Garbage Collection?
Garbage collection (GC) is the process of identifying and disposing of objects that are no longer needed by a program, thereby freeing up memory resources that can be used for other purposes. This helps to prevent memory leaks and manage the memory efficiently.
When no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. However, Garbage collection is not guaranteed to run during the execution of your program. It will not occur simply because one or more objects exist that are no longer used.
Reachability: The GC identifies objects that are no longer reachable by any references in the running program. In other words, if no part of the program can access an object, that object is considered garbage and is eligible for collection.
Mark and Sweep
The most common garbage collection strategy involves two main phases:
Mark: The GC identifies all objects that are still in use (reachable).
Sweep: The GC then frees the memory occupied by objects that are not marked as reachable.
finalize() Method
Sometimes an object will need to perform some action when it is destroyed. For example, if an object is holding resources such as file handle and you want to make sure these resources are freed before an object is destroyed. To handle such situations, Java provides a mechanism called finalization. By using finalization, you can define specific actions that will occur occur when an object is just about to be reclaimed by the garbage collector.
The finalize() method is defined in the java.lang.Object class, which means every class in Java inherits it. It is called by the garbage collector on an object when the object is no longer reachable, just before the object’s memory is reclaimed.
To add a finalizer to a class, you simply define finalize() method. The Java run time calls this method whenever it is about to recycle an object of that class.
The finalize() method syntax:
protected void finalize()
{
// object finalization code
}
The keyword protected is a specifier that prevents access to finalize() by code defined outside its class.
Inside finalize() method you will specify the code that must be executed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by running state or indirectly through other referenced objects. The Java run time calls the finalize() method on the objects before recycling the object.
Purpose of finalize()
The main use of the finalize() method is resource cleanup. The primary purpose of finalize() is to allow an object to perform cleanup operations, such as releasing system resources (e.g., closing files, network connections) before the object is destroyed.
It is important to understand that finalize() is not called when an object goes out of scope. It is called when the GC recycles the object. This means that the invocation of this method is unpredictable and Java cannot guarantee that finalize() will be executed during your program execution. Therefore, you should provide other means of releasing resources.
Java Tutorials
Java Tutorial on this website:
For more information on Java, visit the official website :