Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java / JavaSE6

JAVA Garbage Collection

4.80/5 (8 votes)
20 Dec 2014CPOL3 min read 16.9K  
It's finally time to jump into the wonderful world of memory management and Garbage collection.

Introduction

Today, we are going to look at how Java manages memory in the computer system. So it's time to jump into the wonderful world of Java memory management and garbage collection.

Background

Memory management is a very important concept in Computer Science. Let as think about a program which reads several data as input in a container. After executing some operation on that data, it will write this data to the database. After this execution, the container must be empty in order to process the next batch of data. In languages like C/C++, where we do not find automatic garbage collection, we have to manually take care of these large amounts of garbage data. Else it will cause memory leaks.

Java’s garbage collection has an automatic solution for memory management. Basically, it will free us from adding any memory management logic into our code.

Overview of JAVA Garbage Collection

All the operation of Java's garbage collection runs basically on the heap. The heap is the part of the memory where the Java objects live and it is the only portion of the memory which is actually involved in garbage collection. When the garbage collection runs, its soul purpose is to find and delete those objects that cannot be reached.

Now a million dollar question. When does garbage collection operation run?

The answer is pretty simple “We do not know”. Only JVM will decide when it will run the garbage collection. We cannot force JVM to run garbage collection. We can just request JVM to run garbage collection. But there is no guarantee on that. Normally, JVM will run garbage collector when memory is running low.

Now another million dollar question is: How does Java garbage collector works:

  1. We may hear that JVM uses mark and sweep algorithm in order to do garbage collection.
  2. We may also hear that JVM uses reference counting for garbage collector operation.

But it is not sure that JVM strictly maintains these two rules in order to do garbage collection. Maybe, it follows these two rules may be not. But the main thing is “when an object is eligible for garbage collection?”. The answer is when any live thread cannot access it or reached. So when an object cannot access by a live thread JVM does some magical operation for garbage collection. Reachable object means reachable reference variable that refers to the object in questions. If a Java program has a reference variable and that reference variable actually refers to an object and is available to any live thread, then that object is not ready for garbage collection.

Make Object Eligible for Garbage Collection

There are several ways to make an object compatible for garbage collection. Some of them are:

  1. Nulling a reference
  2. Reassigning a reference variable
  3. Isolating a reference

Nulling a Reference

An object which does not have any reachable reference to it consider for garbage collection. The first way to remove a reference to an object is to set the reference variable that refers to the object to null.

Java
public class NlRef{
    public static void main(String[] args)
    {
        StringBuffer nlref=new StringBuffer("Hello Malisha");
        System.out.println(nlref);
        nlref=null;
    }
}

After creating a string buffer object and doing some operation on it, we are referencing this variable to null. So that "nlref" is eligible for garbage collection.

Reassigning a Reference Variable

Setting a reference variable to refer to another object is another way.

Java
public class Rrvar{
    public static void main(String[] args)
    {
        StringBuffer rrvar1=new StringBuffer("Malisha");
        StringBuffer rrvar2=new StringBuffer("Faizah");
        System.out.println(rrvar1);
        
        rrvar1=rrvar2;
    }
}

Now String buffer “Malisha” is eligible for garbage collection.

Isolating a Reference

There is another situation when a object is eligible for garbage collection even if have a valid reference . This scenario is known as “Isolating a reference”:

Java
public class Car{
    Car cr;
    public static void main(String[] args)
    {
        car c1=new Car();
        Car c2=new car();
        
        c1.cr=c2;
        c2.cr=c1;
        
        c1=null;
        c2=null;
    }
}

After the code executes, it will have some instance variable that is referencing each other. But their link is broken to the outside world. Now these two objects are eligible for garbage collection.

So these are the ways how we actually make an object eligible for garbage collection.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)