Garbag Collection in JAVA (Android Apps) and finalize() Method

Introduction
We can create an object in Java applying new operator. Also, constructors are used to initialize the properties of that object. When an object is no more required, it must be removed from the memory so that the memory can be reused for other objects. Removing unwanted objects or abandoned objects from the memory is called garbage collection (GC). In the languages like C++, GC is performed manually using destructors. But in Java, there is no destructors. In Java, there exist a better mechanism to handle the GC. You need not to delete unwanted objects manually and JVM does this for you. JVM automatically sweeps out abandoned objects from the memory. Before moving on to GC in java, let’s have a look at the finalize() method of Object class.

The finalize() Method
finalize() method is a protected and non-static method of java.lang.Object class. So, this method will be available in all objects you create in java. It is used to perform some final operations or clean up operations on an object before it is removed from the memory. you can override the finalize() method to keep those operations you want to perform before an object is destroyed. Here is the general form of finalize() method.

protected void finalize() throws Throwable
{
//Keep some resource closing operations here
}


Garbag Collection
Whenever you run a java program, JVM creates three threads . 1) main thread 2) Thread Scheduler 3) Garbage Collector Thread. In these three threads, main thread is a user thread and remaining two are daemon threads which run in background.

The task of main thread is to execute the main() method. The task of thread scheduler is to schedule the threads. The task of garbage collector thread is to sweep out abandoned objects from the heap memory. Abandoned objects or dead objects are those objects which does not have live references. Garbage collector thread before sweeping out an abandoned object, it calls finalize() method of that object. After finalize() method is executed, object is destroyed from the memory. That means clean up operations which you have kept in the finalize() method are executed before an object is destroyed from the memory.

Garbage collector thread does not come to heap memory whenever an object becomes abandoned. It comes once in a while to the heap memory and at that time if it sees any abandoned objects, it sweeps out those objects after calling finalize() method on them. Garbage collector thread calls finalize() method only once for one object.

There are some important points:
  • you can call garbage collector explicitly using
    System.gc()
    or
    RunTime.getRunTime().gc()
  • Exceptions occurred in finalize() method are not propagated. They are ignored by the garbage collector.
  • finalize() method on an abandoned object is called only once by the garbage collector thread. GC ignores finalize() method called on an object by the developer.

Comments