What is the Difference Between System gc and Runtime gc in Java

June 2022 · 4 minute read

The main difference between System gc and Runtime gc in Java is that System gc is a class method while Runtime gc is an instance method.

Java is a high-level, general-purpose programming language. It is robust and secure, and provides platform independence and supports multi-threading.  Moreover, it allows object-oriented programing. Therefore, it is easier to model real-world scenarios to build applications using Java. Another important advantage of Java is that it provides a garbage collector, which destroys unused objects automatically to provide better memory management. Running the garbage collector is an automatic process, but it is also possible to request the JVM to run the garbage collector. System gc and Runtime gc are two such methods to request JVM to run Garbage collector.

Key Areas Covered

1. What is System gc in Java
     – Definition, Functionality
2. What is Runtime gc in Java
     – Definition, Functionality
3. What is the Relationship Between System gc and Runtime gc
     – Outline of Association
4. What is the Difference Between System gc and Runtime gc
     – Comparison of Key Differences

Key Terms

System gc, Runtime gc

Difference Between System gc and Runtime gc - Comparison Summary

What is System gc

In the system class, there is a method called gc(). It runs the garbage collector. This method invokes the finalize() method to destroy the objects that are not used.  In Java, the objects are created in the heap.

Usually, the JVM implicitly calls the garbage collector. However, it will not call the garbage collector unnecessarily. Assume that there are only a few objects in the heap. Then, JVM will not invoke the garbage collector. It will only invoke the garbage collector when the heap memory is lower.  

Difference Between System gc and Runtime gc in Java

Figure 1: JVM Architecture

For example, assume that it is required to create a new object and there is a low memory in the heap.  First, the garbage collector will look for an unreferenced object. Then, it will invoke the finalize method. Next, it will release the allocated resources of that object and will destroy the object. Finally, JVM will allocate that memory to the newly created object. This entire process takes more time. Instead, the programmer can call the System.gc() method.  It is a request to the JVM to run the garbage collector continuously in the background.

What is Runtime gc

The task of the Runtime gc is the same as System gc. It is a method that tells the JVM to execute the garbage collector continuously. Unlike Runtime gc, which is a class method, Runtime gc is an instance method.  One major issue in System gc and Runtime gc is that they can cause performance issues.

Relationship Between System gc and Runtime gc

Difference Between System gc and Runtime gc

Definition

System gc is a class method that suggests the Java Virtual Machine (JVM) to run the garbage collector to obtain memory from unused objects to make memory for current objects. Runtime gc is an instance method that suggests the Java Virtual Machine (JVM) to run the garbage collector to obtain memory from the unused objects to make memory for current objects. 

Basis

Therefore, the main difference between System gc and Runtime gc is that the system gc is a class method whereas runtime gc is an instance method.

Type

Another difference between System gc and Runtime gc is that system gc is a non-native method while runtime gc is a native method.

Declaration

Declaration for java.lang.System.gc() method is public static void gc()

Declaration for java.lang.Runtime.gc() method is public void gc()

Conclusion

Garbage collector releases the heap memory by destroying the unreachable objects. System gc and Runtime gc are two methods to request JVM to run Garbage collector. The basic difference between System gc and Runtime gc in Java is that System gc is a class method while Runtime gc is an instance method. Usually, System gc is more convenient than Runtime gc.

Reference:

1. What Is the Use of System.gc()? | Core Java Interview Questions, Naresh i Technologies, 1 Mar. 2017, Available here.
2. “Java.lang.System.gc() Method Example.” Www.tutorialspoint.com, Available here.
3. “Java.lang.Runtime.gc() Method Example.” Www.tutorialspoint.com, Available here.
4. “Garbage Collection in Java.” GeeksforGeeks, 7 Sept. 2018, Available here.

Image Courtesy:

1. “Java virtual machine architecture” By Dcoetzee (File:Computer-blue.svg by OpenClipart, File:Phone-blackberry-bold.svg by User:Techtoucian) – Own work (CC0) via Commons Wikimedia

ncG1vNJzZmiolZm2oq2NnKamZ6edrrV5yKxkraCVYrGqssWeqZ6mk5p6o7HTsJyepl2oxrTAxKZkoJtdlruledGupa2hnZp6qK%2BMoqVmopGrrnA%3D