Garbage Collection in java
Garbage Collection in java
Garbage means unreferenced objects.
Garbage collector is responsible for collect unused objects.
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.
The process of removing unused objects from heap memory is known as Garbage collection.
We know that objects are dynamically allocated by using the new operator and
objects are destroyed and their memory released for later reallocation. In some
languages, such as C++, dynamically allocated objects must be manually released by use of a delete operator.
In Java, garbage collection done automatically for you.
Steps required to perform garbage collection:
1.The unreferenced objects are identified and marked as ready for garbage collection.
2.The marked objects are deleted by the garbage collector.
3.optionally,memory can be compacted after garbage collector deletes objects,so remaining objects are in a contiguous block at the start of the heap.
Advantages/Benefits of Garbage Collection:
1.It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.
2.It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
When does java perform garbage collection?
1. When the object is no longer reachable
2. When one reference is copied to another reference
3.Anonymous object
Or
How can an object be unreferenced ?
There are 3 ways:
1.By nulling the reference
2.By assignimg a reference to another
3.if the object is an anonymous object.
1. By nulling the reference
Example:
Student s=new Student();
s=null;
2.By assignimg a reference to another.
Example:
Student s1=new Student();
Student s2=new Student();
s1=s2;
3.if the object is an anonymous object.
Example:
new Student();
How to request the jvm to run the garbage collector?
->once we made object eligible for garbage collection,it may not be destroyed immediately by the garbage collector.
->we can also request the jvm to run garbage collector.
Two ways to garbage collection.
1.using System.gc() method:
The gc() method is used for invoke the garbage collector to perform cleanup processing.
The gc() is found in system & Runtime classes.
2.using finalize() method:
It is invoked each time before the object is garbage collected.This method is used to perform cleanup processing.
Just before destroying an object garbage collector calls finalize method() on to perform cleanup.Once finalize method completes,garbage collector destroys that object.
Syntax:
protected void finalize() throws Throwable
{ //finalize-code }
Program to demonstrate java garbage collection.
public class Example
{
public static void main(String[] args)
{
Example e= new Example();
e=null;
System.gc();
}
public void finalize() {
System.out.println("Garbage Collected");
}
Output:
Garbage collected
How we can force the garbage collection explicitly?
Garbage collection automatic process & can't be forced .we could request it by calling system.gc()method. Jvm deos not guarantee that garbage collector will be started immediately.
Comments
Post a Comment