Let's learn java programming language with easy steps

Tuesday, 20 June 2017

Java Garbage Collection


Garbage Collection

Java Garbage Collection

Garbage collection, Here garbage means unused or unreferenced objects. 

In java, Garbage collection is the way to destroy all the unused and unreferenced objects from the heap memory and garbage collection are performed by the JVM automatically. We do not need to write any extra code in our java program.

If we want to perform garbage collection in c language and c++ then we have to use free() function in c language and delete() function in c++ but in java it is performed automatically, so java provides batter memory management and it make memory efficient.

Advantage of Garbage Collection

There are some advantage of garbage collection in java. These are
  • It makes the memory efficient because garbage collector destroy all the unused and unreferenced objects from the heap memory.
  • It saves time because the garbage collection is performed by the JVM so we do not need to write any extra code in java programs.
  • It reduces the length of the code because it is handled by the JVM, so we don't need to write extra code in prorgram.
  • Programmer does not need to worry about dereferencing an objects.

When an Objects Becomes eligible for Garbage Collection

There are many ways when objects become unused and unreferenced in java. these are
  1. By nulling the reference.
  2. By assigning a reference to another.
  3. By anonymous objects etc.

1) Example of by nulling a reference

Student s = new Student();
s = null;

2) Example of by assigning a reference to another

Student s = new Student();
Student s1 = new Student();
s = s1;

3) Example of by using anonymous object

new Student();

Finalize Method

The finalize() method comes from Object class which is available in java.lang package. 

In java sometimes object will need to perform some specific task before it garbage collected such as open and closing connection etc. To handle this situation finalize() method is used.

Basically finalize() method is used to clean-up processing.

Before the object is garbage collected, finalize() method invoked each time.

Syntax of finalize() method :

protected void finalize(){}

The finalize method is called by the garbage collector thread of JVM. Garbage collector thread is Daemon thread.

In java garbage collection is performed by the Daemon thread called garbage collector (gc).

gc() Method

The gc() method is used to invoke garbage collector to perform cleanup processing. The gc() method is defined in the System and Runtime classes.

Java Garbage Collection Example

public class GarbageCollectionTest
public void finalize()
System.out.println("object is garbage collected");
public static void main(String args[])
GarbageCollectionTest g = new GarbageCollectionTest();
GarbageCollectionTest g1 = new GarbageCollectionTest();

g = null;
g1 = null;


output : object is garbage collected
              object is garbage collected

Q. Can garbage collection be the forced explicitly ?

Ans. No, Garbage collection cannot be the forced explicitly but we may request to the JVM for garbage collection. There is no guarantee that JVM will perform garbage collection its totally depend on JVM. We can only request to the Java Virtual Machine(JVM).



Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email