Garbage Collection in Java


Garbage collection was one of the major concerns for the early java developers and it has come out to be one of the finest achievements in the field.

We are pretty much fearless in creating and using the java objects while actual development, without actually worrying about explicit memory allocation and de-allocation in the JVM heap space, as and when required. The superb thing is that the JVM Garbage Collector does it all for us behind the scene, thus eliminating the unexpected memory related issues.

How does the Garbage Collector work?

Let’s take a brief tour of how the Garbage Collector works for us.

Firstly, it tracks all the live objects, that is – all those objects that have live references to them, which are being used one or the other way in the application. Everything apart from these live objects are nothing but the garbage or the un-referenced objects.

There is a portion of system’s memory that is allocated to the JVM in advance. This allocated memory to the JVM is known as the Heap memory, which is managed by the JVM itself. So there are few important points that we need to keep in mind regarding the memory allocation/de-allocation process by the JVM.

  1. Whenever an object is created, the memory allocation for it is done in the Heap memory.
  2. Assume that if yet another object is created, very similarly another portion of the heap memory gets allocated for it, starting from the offset pointer (Offset pointer marks the most recent memory allocation level).
  3. Now suppose that when an object is deleted or removed, the Garbage Collector gets its task assigned to reclaim the deleted object’s underlying memory and keeps it reserved for any future allocation. This means that there is no explicit de-allocation of the memory giving it back to the operating system.

What type of objects are eligible for being garbage collected?

  1. Assigning the reference variable as null –
  2. Pointing the reference to some other object –
  3. Whenever we create an object anonymously as –

There are few more points to take note of regarding Garbage Collection

  1. The Garbage Collector collects only those objects that have been created using the new keyword.
  2. There is a finalize() method that gets called by the Garbage Collector on an object when it determines that there are no more existing references to the object. However, it best not to rely on the finalize() method, as it’s not guaranteed.
  3. There is another method System.gc() which is capable of invoking the Garbage Collector.

Island of Isolation

Basically, there are instances where a group of objects that reference each other, but they are nowhere referenced as any active object in the application. For example – Object A has a reference to Object B. Object B has a reference Object A. Also, neither Object A nor Object B is referenced by any other object. Then that’s an Island of Isolation.

Note that, obj1 and obj2 reference variables are set to null, but still the Person objects are referenced by two reference variables obj1.b and obj2.b. These Person objects cannot be accessed anymore from anywhere and hence have become an Island of Isolation.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.