(Download One Minute Java Mobile app for latest updates)
Garbage Collector :
As the name suggest garbage collector helps to perform cleaning activity of discarded objects. Discarded objects are those objects which are not reachable by any live thread.
Below image will help you understand this better :
Garbage Collector :
As the name suggest garbage collector helps to perform cleaning activity of discarded objects. Discarded objects are those objects which are not reachable by any live thread.
Below image will help you understand this better :
When is object eligible for garbage collection?
To answer this in one line, as soon as life of last variable referring to object ends, object become eligible for garbage collection.
Very obvious question will come to your mind, and when dose the life of variable ends? to understand this check following example.
However in some cases, if method returns a reference of local variable, than even if the life of local variable ends, object will still not be eligible for garbage collection.
So to conclude, if we declare all our variables judiciously we need not want to worry about garbage collection, except in certain scenarios which we will discuss latter.
But however in some cases, if you wish to explicitly make object eligible for garbage collection you can take following steps.
How to explicitly make object eligible for garbage collection?
Below are some ways to explicitly make object eligible for garbage collection.
1. By making reference null
2.
By setting a reference variable, refer to another object
What is "island of isolation" in java?
Please refer below image.
When does garbage collection starts cleaning process ?
Answer to this question is, there is no assured way when this cleaning process will start it varies from JVM to JVM and the underlying platform.
Can we force garbage collection?
We cannot force garbage collection, but we can request with the help of below methods. Once again we cannot invoke garbage collection by these methods, we are just requesting it whether to fulfil your request depends solely on JVM.
1. System.gc();
2. System.runFinalization();
The only method that claim to guarantee force garbage collection is System.runFinalizersOnExit() but this method has many limitations and due to this it is deprecated.
No comments:
Post a Comment