Line of behavior of the garbage collector (memory reclaimer)
The Java programmer does not need to monitor memory allocation because the garbage collector manages memory automatically. The Garbage Collector is run by the Java Virtual Machine (JVM). The garbage collector is a low-priority process that runs periodically and frees memory used by objects that are no longer needed. Different JVMs have different garbage collection algorithms. There are several algorithms used, for example: reference counting algorithm or markup and scraping algorithms.
Running the garbage collector in Java
The JVM typically runs the garbage collector when free memory is low. But the operation of the garbage collector does not guarantee that there will always be enough free memory. If there is not enough memory even after recovery, the JVM throws an OutOfMemoryError exception. Please note that the JVM must run the garbage collector at least once before throwing an exception. You can request the garbage collector to run in Java, but you cannot force this action.
Request to run the garbage collector
To make a request, you can call one of the following methods:
System.gc()
Runtime.getRuntime().gc()
Suitability to run garbage collector
An object must be disposed of when it is no longer available to the living stream. An object may be subject to disposal in different cases:
- If a variable of a reference type that refers to an object is set to "0", the object must be disposed of if there are no other references to it.
- If a variable of a reference type that refers to an object is created to refer to another object, the object must be disposed of if there are no other references to it.
- Objects created locally in a method are discarded when the method exits, unless they are exported from that method (that is, returned or thrown as an exception).
- Objects that reference each other may be subject to disposal if none of them are available to the live thread.
Let's look at an example:
public class TestGC
{
public static void main(String [] args)
{
Object o1 = new Integer(3);
Object o2 = new String("Tutorial");
o1 = o2;
o2 = null;
}
}
In this example
Integer
, the object (integer) originally referenced by pointer o1 can be disposed of after line 3 because o1 now refers to the object
String
(string). Even though o2 is created to refer to null, the object
String
(string) is not recyclable since o1 refers to it.
Finalization
Java technology allows you to use a method
finalize()
(finalize) to do the necessary cleanup before the garbage collector retrieves an object from memory. This method is called on an object by the garbage collector when the garbage collector determines that there are no more references to the object. This is described in the class
Object
, which means it is inherited by all classes. The subclass overrides the method
finalize()
to free itself from system resources or for another cleanup:
protected void finalize() throws Throwable
If an unregistered exception is thrown by the method
finalize()
, the exception is ignored and finalization of that object stops. The method
finalize()
will only be invoked once during the object's lifetime. It is possible to use a method
finalize()
on any object to protect it from disposal. But in this case, the garbage collector is no longer activated
finalize()
for this object. The method
finalize()
will always be invoked once before the object is garbage collected. However, it is possible that the method
finalize()
will not be activated for a given object for the entire duration of its existence, since it may not be subject to disposal.
Summary
In this section, we looked at the garbage collection process, which is a memory management technique in the Java language. Garbage collection cannot be forced. We learned about the different ways to make objects eligible for recycling and learned that the method
finalize()
is invoked before the object is reclaimed by the garbage collector.
Exercise
Question: How many objects will be subject to disposal after line 7?
public class TutorialGC
{
public static void main(String [] args)
{
Object a = new Integer(100);
Object b = new Long(100);
Object c = new String("100");
a = null;
a = c;
c = b;
b = a;
}
}
Answer options: A. 0 B. 1 C. 2 D. 3 E. The code cannot be compiled
Correct option: B
Explanation: Of the three objects created in lines 1, 2 and 3, only the object
Integer
must be disposed of at the end of line 7. Variable references, a, which originally referenced the object
Integer
, references the object
String
on line 5. Thus,
Integer
the object must be disposed of after line 5, since there are no variables that reference it. Variables
b
and
c
refer to objects
String
and
Long
objects in lines 6 and 7, so they are not recyclable.
GO TO FULL VERSION