Java gc()
Garbage collection
Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced and destroyed to free up memory space. The task is performed by a garbage collector that resides inside the JVM (Java Virtual Machine). When dereferenced objects are shifted from the young generation to the old generation, the garbage collector is executed.
The JVM can perform garbage collection operation during the execution program. But we don’t know when it’s going to be executed. Hence, we can make JVM run a garbage collector at least once throughout the execution of the program by using System.gc() method.
How to mark an object for garbage collection?
There are different ways to make an unused object eligible for garbage collection.
- By making a reference null
- Assigning an old reference to new reference
- With the use of anonymous object
All these methods are explained in the previous article about Java garbage collector.
Ways to implementgc() method
- Using System.gc() Method
System.gc() is a method available in java.lang package. It can be invoked by using simple syntax.
Syntax:
System.gc()
DemoGC.java
public class DemoGC { public static void main(String a[]) { DemoGC d = new DemoGC(); /* marking an object d for garbage collection */ d=null; /* call to gc() method */ System.gc(); } @Override public void finalize() { System.out.println("Garbage collector method executed for object: "+this); } }
Output:
Garbage collector method executed for object: DemoGC@7978f9b4 Garbage collector method executed for object: DemoGC@77bf18eb
In the above code, a class DemoGCis declared. An object d of the class DemoGC is marked for garbage collection by marking d as null. After that System.gc() method is called.
The output contains the object id that is destroyed by the gc() method.
- Using Runtime.getRuntime.gc() method
The Runtime class permits the application to make an interface with the JVM. And it executesgc() method to perform garbage collection.
GcExample.java
public class GcExample { public static void main(String a[]) { GcExample d1=new GcExample(); GcExample d2=new GcExample(); /* marking object d1 for garbage collection */ d1=d2; /* call to gc() method */ Runtime.getRuntime().gc(); } @Override public void finalize() { System.out.println("Garbage collector method executed for object: "+this); } }
Output:
Garbage collector method executed for object: DemoGC@7978f9b4
In the above code, a class DemoGCis declared. Two objects d1 and d2 are declared. An object d1of the class DemoGC is marked for garbage collection by assigning the old reference to new reference. After that System.gc() method is called.
The output shows the object id that is destroyed by the gc() method.
Advantages of gc() method
- When an application or server is started many objects are getting invoked. After that those object needs to be finalized. At this time we can call System.gc() method to clear up the memory space.
- System.gc() method can be used while debugging the program. To check whether memory leak is not happening in the system.
Disadvantages of gc() method
- The gc() method may execute for unlimited time. Itimpacts the further execution of the program. Ultimately affecting the performance of program.
Example of using gc() method
The below code counts the number of staff working in the hotel (excluding new staff).The concept of a garbage collector is used for this program.
Hotel.java
/* code to count total number of Hotel staff excluding new employees. */ class Hotel { private int empid; private String empname; private int empage; private static int nextId=1; /*it is made static because it is shared by all objects*/ public Hotel(String empname,int empage) { this.empname = empname; this.empage = empage; this.empid = nextId++; } public void show() { System.out.println("Staff ID="+empid+"\nName="+empname+"\nAge="+empage); } public void showNextId() { System.out.println("Next staff id will be="+nextId); } protected void finalize() { --nextId; /*In this case, gc will call finalize() for 2 times for 2 objects.*/ } }// End of Hotel public class HotelMaster { //main() public static void main(String a[]) { Hotel h1=new Hotel("Staff1",42); Hotel h2=new Hotel("Staff2",36); Hotel h3=new Hotel("Staff3",30); h1.show(); h2.show(); h3.show(); h1.showNextId(); h2.showNextId(); h3.showNextId(); { /* this contains the new staff entries and again marking them for garbage collection. */ Hotel e1=new Hotel("Staff4",22); Hotel e2=new Hotel("Staff5",24); e1.show(); e2.show(); e1.showNextId(); e2.showNextId(); e1 = e2 = null; System.gc(); System.runFinalization(); } h1.showNextId(); } }//End of HotelMaster
Output:
Staff ID=1 Name=Staff1 Age=42 Staff ID=2 Name=Staff2 Age=36 Staff ID=3 Name=Staff3 Age=30 Next employee id will be=4 Next employee id will be=4 Next employee id will be=4 Staff ID=4 Name=Staff4 Age=22 Staff ID=5 Name=Staff5 Age=24 Next employee id will be=6 Next employee id will be=6 Next employee id will be=4
In this way, this article helps to learn more about System.gc() method used for garbage collection in Java.