Garbage Collection In Java

JVM with its built-in garbage collection functionality is a boon to Java developer. It allows developer to program without worrying much about memory management, because Java Garbage Collector will take care of it. This is unlike most programming languages like C, C++ etc. where the programmer need to handle memory management as well. 

Garbage Collector in Java automatically reclaims the allocated memory for reuse. It traces the live objects and keeps garbaging unreferenced objects.

Heap and Memory Leak

Java objects are created in the Heap irrespective of their scope. Class variables and static members are created in method area of Java memory space. Both heap and method area is shared between different threads. In this article, we will talk about Heap Area which is used for dynamic memory allocations. Most of the Operating Systems allocate the heap in advance to be managed by JVM while the program is running.

When an object is created, JVM allocates a portion of heap space and moves the offset pointer, so the next allocation will be at this offset. When the object is no more referred, therefore unreachable by the application code, Garbage Collector retrieves the underlying memory and reuses it for future allocation.

There are possibilities to have unused objects that are still reachable by application code which the developer might have forgotten to dereference. Such objects are neither used nor can be garbage collected which leads to a situation called memory leak.

Garbage Collection Roots

Garbage Collector starts its work by looking at set of references called as 'Garbage Collection roots' which are always reachable by JVM and contain references to objects created by the underlying application. Every object in heap must be refered by atleast one 'GC root' object. When garbage collection starts, it marks these objects as live object and then looks at any objects that they have reference. And so it continues till it reaches top level and no further objects are refered. The GC roots includes system classes (loaded by bootstrap/system), local variables, main thread, etc.

Git Branching Support

JVM uses a Garbage Collector called Tracing Collector which pause all the surrounding root objects(objects referenced directly by running threads), their references and every objects it sees on its way.

Generational Garbage Collector

Java implements generational Garbage Collector based upon the Generational Hypothesis which states that the majority of objects that are created are quickly discarded, and objects that are not quickly collected are likely to be around for a while. Based on this, Java partitions memory area into different generations and operates differently upon them.

The different generations are:

  1. Young Generation
  2. Tenured Generation
  3. Permanent Generation
Git Branching Support

Young Generation

It has two sub-generations: Eden Space and Survivor Space. Survivor Space has two partitions S0 and S1.

Eden Space: When objects are created, they first reside in Eden Space of heap memory area. Here the Garbage Collector discards the unreferenced objects and migrates the live objects to one of the Survivor Space.

Survivor Space: Any one of the Survivor Space is in use at any given point of time. Objects that are alive in Eden Space are moved to Survivor Space during garbage collection.

The Young Generation is handled by a tracing & copying collector called as "Parallel New Collector". When Eden Space fills up, Garbage Collector pauses the world around it and then traces through the objects in the young generation. Objects those are marked alive are moved to Survivor space. All unreferenced object in Eden Space will be discarded and it will now be ready to be filled up with new objects. The next garbage collection cycle will use the other Survivor space.

Tenured Generation

This space is also called as 'Old Generation'. Older objects with longer lifetimes are moved over from the survivor space into this area. When this space is filled up, full garbage collection is performed. JVM throws 'OutOfMemoryError - Java Heap Space' when this space is overflowed.

Old Generation is the last phase of Garbage Collection Process. 'Major Garbage Collector' is used to scans this heap space. If instances are dereferenced, they are discarded otherwise continue to stay in the Old Generation.

Permanent Generation

Also called as 'Perm Gen Space'. It is not a part of the Java Heap memory. It contains metadata required by the JVM to describe the classes and methods used in the application. It is populated at runtime based on the classes used by the application. These objects are GC in a 'full garbage collection'.


2 ways to Create a Thread in Java

Threads in Java can be created in 2 different ways, extending the Thread class and implementing the Runnable interface. This article explains the two ways with suitable examples.

View Article

4 Different Ways of Creating an Object in Java

Most of us have created object using new operator in Java. Did you know the other ways to create objects in Java. This article talks about 4 different ways of creating objects in Java.

View Article

Using AutoCloseable with 'try-with-resources'

Java 7 added the java.lang.AutoCloseable interface that developers can implement in their custom classes for using them with try-with-resources.

View Article

Chained Exception

Chained exception is a feature introduced with JDK 1.4, which allow developers to associate an exception with another exception.

View Article

Shallow Cloning and Deep Cloning

Cloning an object can happen in two ways Deep and Shallow Cloning. In shallow cloning only object references are copied while in deep copy actual objects are copied to the newly cloned object.

View Article

Exception Handling in Inheritance

There are certain rules that must be followed while throwing exceptions in inheritance and overriding methods. This article discuss about these rules.

View Article

Understanding static and final in Java

Java provides various modifiers which when used with variables, methods or classes, provides them with special properties. Static and final are are two such modifiers which are used frequently by Ja..

View Article

Understanding Object Cloning

Java provides the clone() method which can be used to create a copy an object. It creates a new memory location and copies the content of the object being cloned into the new location.

View Article

Try-with-resources Statement in Java 7

Java 7 introduces the new 'try-with-resources' statement which help developers to overcome the tedious task of closing the resources that have been used in the program.

View Article

Varargs: Variable Arguments to Methods

Java 5 introduced a new feature called varargs, which allows methods to accept variable number of arguments. They are useful in cases where the number of arguments to be passed to a method is unknown.

View Article