Garbage Collection In Java
Garbage Collection In Java is a process to free unused memory. Garbage collector in Java free only one type of memory and this is dynamic memory allocation.
Garbage collector free the memory which is not being used by the program and the memory which is reserved by the programmer.
When Garbage collector performs garbage collection then only reserve tag is removed and that the data is still there until it is overwritten by some other data.
If garbage collection is not automatic in Java then a lot of out of memory problem may arise. As in java everything is based objects and if Garbage Collector is not automatic then heap area will become full and out of memory errors will come.
There are two types of objects in Java:
· Reachable
· Unreachable
In Java only unreachable objects are cleared by Garbage collector. So, when these conditions arise when the object become unreachable?
There are four conditions in Java when the objects become unreachable:
1) When we create object in functions other than main:
This condition arises 95% of time. Consider the following program to understand this condition in detail:
class Demo
{
int x = 10;
int y = 20;
static void show()
{
Demo d2 = new Demo();
display();
}
static void display()
{
Demo d3 = new Demo();
}}
class Temp
{
public static void main(String[] arg)
{
Demo d1 = new Demo();
d1.show();
}}
When the program will finish executing, the variables d2 and d3 become unreachable but the content in main is still running and in the memory.
So we should always make objects in functions other than main() to make your program more efficient and GC(garbage collector) friendly.
2) when we assign one object’s RID to another:
class Demo
{
int x =10;
int y=20;
}
class Temp
{
public static void main(String[] s)
{
Demo d1 = new Demo();
Demo d2 = new Demo();
//here we assigned the Reference inside d1 to d2
d2 = d1;
}}
3) if we set RID of an object to null:
If we set the reference ID of any object to null then it becomes unreachable.
class Demo
{
int x = 10;
int y = 20;
}
class Temp
{
public static void main(String[] s)
{
Demo d1 = new Demo();
System.out.println(d1.x);
d1 = null;
}}
4) fourth conditions is By making anonymous objects.
As we already discussed many times in the past that anonymous objects have two main properties:
1. they do not have any name
2. they cannot be reused
So whenever we define an anonymous object then it becomes unreachable after first use.
class Demo
{
int x = 10;
int y = 20;
}
class Temp
{
public static void main(String[] s)
{
System.out.println(newDemo().x); //anonymous object
}}
*************************___****************************
Now the question arises that how this JVM finds which object has lost its Reference. or How it identifies that which memory has lost its RID?
The Garbage Collector Program runs parallel to our main thread and checks in main thread for lost RIDs. But HOW?
So, the answer is that every thread maintains its own stack and once a particular stack completes then the GCP(garbage collector program) runs and check in a separate stack whether there is still Reference or memory or not. If it is lost then GCP clears its object from the memory.
Diagrammatic representation of this process:
***********************____***********************************
There are four types of references of objects in Java:
1. Strong Reference: It is direct reference like we normally use, as in
“integer i = new integer(13);”
“integer i = new integer(13);”
2. Weak reference: Weak references are weaker than soft ref. If the only reference to an object is weak reference, the garbage collector can reclaim the object’s memory at anytime. It does not have to wait until the system runs out of memory. Usually, it will be freed next time the garbage collector runs.
3. Soft reference: Soft references are like a cache. When memory is low, the garbage collector can arbitrarily free up soft ref. If you are using soft ref then garbage collector is required to free them all before throwing an out of memory exception.
4. Phantom reference: Phantom references are a special reference that allow you to be notified before the finalization and frees the object. Think of it as a mechanism to perform clean up.
************___**************___*********************
Some Important Questions:
Question: What kind of objects are free by the garbage collector automatically?
Answer: Only unreachable objects are cleared in Garbage collector.
Question: What is an unreachable object?
Answer: Those objects are called unreachable whose RID(reference ID) is lost from the program.
Question: Can you get back the reference of an unreachable object?
Answer: Yes, we can do this if we can get object just before the garbage collector runs.
We can regain the reference of an unreachable object by catching the reference of object in “this” keyword.
class Demo
{
static Demo z;
int x;
int y;
public void finalize() throws Throwable
{
System.out.println("The End");
z = this;
}}
But this can be done only once. Second time JVM will destroy its reference without invoking finalize because the JVM follows mark and sweep algorithm.
Question: If we call the finalize method explicitly then what will happen?
Answer: Nothing happens, function will run normally.
Question: Can you force JVM to start garbage collection process explicitly?
Answer: No, we cannot force. we can request only. If the JVM will find the heap size low on memory then it will start the garbage collection automatically.
No comments:
Post a Comment