Published on Oct 19, 2022
Garbage Collection in Java is a process by which the programs perform memory management automatically. The Garbage Collector (GC) finds the unused objects and deletes them to reclaim the memory. In Java, dynamic memory allocation of objects is achieved using the new operator that uses some memory and the memory remains allocated until there are references for the use of the object. When there are no references to an object, it is assumed to be no longer needed, and the memory, occupied by the object can be reclaimed. There is no explicit need to destroy an object as Java handles the de-allocation automatically. The technique that accomplishes this is known as Garbage Collection. Programs that do not de-allocate memory can eventually crash when there is no memory left in the system to allocate. These programs are said to have memory leaks. Garbage collection in Java happens automatically during the lifetime of the program, eliminating the need to de-allocate memory and thereby avoiding memory leaks.
Java garbage collection is an automatic process. Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting unused objects. An in-use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused or unreferenced object is no longer referenced by any part of your program. So, the memory used by an unreferenced object can be reclaimed. The programmer does not need to mark objects to be deleted explicitly. The garbage collection implementation lives in the JVM.
Two types of garbage collection activity usually happen in Java. These are:
Minor or incremental Garbage Collection: It is said to have occurred when unreachable objects in the young generation heap memory are removed.
Major or Full Garbage Collection: It is said to have occurred when the objects that survived the minor garbage collection are copied into the old generation or permanent generation heap memory are removed. When compared to the young generation, garbage collection happens less frequently in the old generation.
Unreachable objects: An object is said to be unreachable if it doesn’t contain any reference to it. Also, note that objects which are part of the island of isolation are also unreachable.
Unreachable objects: An object is said to be unreachable if it doesn’t contain any reference to it. Also, note that objects which are part of the island of isolation are also unreachable.
Example:
Integer i = new Integer (4);
// The new Integer object is reachable via the reference in ‘i’
i = null;
// The Integer object is no longer reachable.
Eligibility for garbage collection: An object is said to be eligible for GC (garbage collection) if it is unreachable. After i = null, integer object 4 in the heap area is suitable for garbage collection in the above image.
Using System.GC () method: System class contain static method GC () for requesting JVM to run Garbage Collector.
Using Runtime. get runtime().gc() method: Runtime class allows the application to interface with the JVM in which the application is running. Hence by using its GC () method, we can request JVM to run Garbage Collector. There is no guarantee that any of the above two methods will run a Garbage Collector. The System. GC () is effectively equivalent to the call Runtime. Get Runtime (). GC ().
The GC () method is used to invoke the garbage collector to perform clean-up processing. The GC () is found in the System and Runtime classes. public static void GC () {}
By nulling the reference
Employee e=new Employee();
e=null;
By assigning a reference to another
Employee e1=new Employee();
Employee e2=new Employee();
e1=e2;//now the first object referred by e1 is available for garbage collection
By anonymous object etc.
new Employee();
package uk.lset.accessmodifier.pack1;
public class TestGarbage1{
public void finalize () {
System.out.println(“object is garbage collected”);
}
public static void main (String args[])
{
 TestGarbage1 s1=new TestGarbage1();
 TestGarbage1 s2=new TestGarbage1();
 s1=null;
 s2=null;
 System.gc();
}
}
Output: object is garbage collected
The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites
Join global tech minds at LSET for a hands-on journey into AI & Machine Learning Innovation.
Limited Seats Sign Up Today!
This will close in 0 seconds