Java Garbage Collection

Hello everyone!!! Today in this post you will learn java garbage collection. First let us understand what exactly Garbage Collection is, in Java??

Java Garbage Collection

Java Garbage Collection is the process of deleting unused objects in heap memory. Garbage collection in java is automatic. In this process compiler identifies the objects which are in use and which are not and deletes the unreferenced objects that is no longer referenced by any part of program.

Garbage Collection Example:

Java Garbage Collection

Also Read¬†–¬†Getting IP Address of Current Machine Using Java Program

import java.util.*;

public class Garbage_Collection {

public static void main(String[] args) {

Runtime rt = Runtime.getRuntime();
System.out.println("Before garbage collection: Available free memory in JVM = " + rt.freeMemory());
System.out.println("After garbage collection: Available free memory in JVM = " + rt.freeMemory());

Here i have not shown the output since the numbers vary from one system to another. Obviously numbers are not important. The fact we have to consider here in java garbage collection example is memory available is more than before.

About the author


View all posts

Leave a Reply

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