• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

System.gc Invocation For Garbage Collection

March 18, 2014 //  by Krishna Srinivasan//  Leave a Comment

Garbage collection is the process of reclaiming the used memory which is no more reference objects in the application. Java Virtual Machine (JVM) runs the background thread for monitoring the object references and runs periodically to claim the memory. This thread runs on the low priority since the performance of JVM will be impacted while running this thread which stops all other threads for few seconds. System.gc()  is the method which suggests the garbage collection to the JVM. However, this is not guaranteed.

System.gc() is implemented by the VM, and what it does is implementation specific. The implementer could simply return and do nothing, for instance.

Look at the below simple example.

package javabeat.net.system;
import java.lang.*;
public class GCDemo {
   public static void main(String[] args) {
     int arr1[] = { 0, 10, 20, 30, 40, 50 };
     int arr2[] = { 0, 100, 200, 300, 400, 500 };

     System.arraycopy(arr1, 0, arr2, 0, 1);
     System.out.print("array2 = ");
     System.out.print(arr2[0] + " ");
     System.out.println(arr2[1] + " ");

     // it runs the GarbageCollector
     System.gc();
     System.out.println("Cleanup completed...");
   }
}

If you look at the above example, there is chance of calling the JVM to reclaim the memory. However, it is not guaranteed to run at the specified time. Also it depends on the JVM implementation vendor who chooses the appropriate time for calling the garbage collection. The Java Language Specification does not guarantee that the JVM will start a GC when you call System.gc(). This is the reason of this “may or may not decide to do a GC at that point”.

Now, if you look at OpenJDK source code, which is the backbone of Oracle JVM, you will see that a call to System.gc() does start a GC cycle. If you use another JVM, such as J9, you have to check their documentation to find out the answer. For instance, Azul’s JVM has a garbage collector that runs continuously, so a call to System.gc() won’t do anything

Category: JavaTag: Java System

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « NullPointerException in Java
Next Post: HTML5 Small Tag »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact