Java Garbage Collection

Hello everyone!!! Welcome to flower brackets blog. Today in this post you will learn java garbage collection.

Java Garbage Collection

Why do we need garbage collection in Java?

Before java, most popular programming language was either C or C++. So if you know these languages you would know that you are supposed to manage your own memory in those languages.

For example, in C you have methods like calloc(), malloc(), realloc() that would allow buffer memories for you. You have to specify how much memory you need for your program and say that in those API calls and then can get you a buffer of memory for doing something like creating a linked list node or something like that.

But at the end of your program, at some logical point you are responsible for freeing that memory as well. So, let’s imagine a big application written in C language that keeps on allocating buffer memories and sometimes it forgets to free the memory and eventually they’ll cause memory leaks and lot of issues in the application.

Also in C++, you would use constructors like, new and destructors to release resources. So those are some things that programmers had to take care or worry about whenever they were implementing things in languages like C and C++.

But java came with a automatic memory management through a thread called garbage collector and its primary objective is to free heap memory by destroying unreachable objects.

What are unreachable objects in java? When an object becomes eligible for garbage collection?

Unreachable objects are those objects which do not have live references. Let’s see an example,

public static void main(String[] args)
   // StringBuffer object sb is not eligible for garbage collection
   StringBuffer sb = new StringBuffer("Flower Brackets");
   // StringBuffer object sb is eligible for garbage collection
   sb = null;

In the main method I have created StringBuffer object and its reference. I’m going to print this StringBuffer object. At this stage StringBuffer object is not eligible for garbage collection.

Now I’m going to assign value “null” for StringBuffer object. In this case StringBuffer object is eligible for garbage collection and becomes unreachable object in the heap memory.

So, it basically works on a couple of hypothesis which is, that most of the objects soon become unreachable, that means the objects are generally created in context of “if block” or a method.

So most of the objects are created like that to write your code and those objects are out of scope as soon as the method execution is complete, those can be garbage collected.

Next is references from old objects to new objects only exists in small numbers. That means, objects that live for long time in your application doesn’t generally refer to newly created objects.

Here are couple of terms that we should be familiar with, one is live object. Live object is an object in your application that is referred by another object in your application.

Another is “dead” object. Dead object is unreachable object which is created during a method call and as soon as the method call is over the object became out of context and just lying there in heap.

So those are the unreachable objects.

Here are some basics before learning garbage collection

Basically objects are allocated in the heap. They’re new, whenever you call a new and allocate an object that is allocated in the heap.

Also arrays are object in java, hence allocate memory in heap. Things like static member class definitions, class metadata etc. are stored in  area of the JVM.

Garbage collection is carried out by daemon thread called garbage collector. There is a API available in java called System.gc(), a static method call on the system class.

Calling System.gc() method may or may not trigger gc() method which sometimes is the case, but there is no guarantee that would happen.

So we cannot force java to do the garbage collection by calling System.gc() method. Moving to next point which is java.lang.OutOfMemoryError.

This is a runtime error that you would get if JVM is no longer able to allocate new object in the heap. So you will get an error which clearly tells what happened.

What is garbage collection in java?

Garbage collection in java means destroying/de-allocating unused objects in memory. Objects which are not used or has no references can be reclaimed.

Garbage collector is used to reclaim unused memory automatically. Java handles memory de-allocation automatically since object once created uses some memory in the heap section till there are references to that object.

Advantages of garbage collection

  1. Garbage collection in java happens automatically, since java relieves you from additional burden of releasing used memory. Hence making java memory efficient.
  2. Garbage collection ensures program integrity.
  3. We don’t have to write extra code since garbage collector is the part of JVM.

There are two methods that can be used in java garbage collection, they are

  • finalize() method
  • gc() method

Related Posts