Garbage Collector : finalize
In C/C++ or all the languages before java we used to manually free the unused memory by deleting the objects/reference created
so that system can use that memory for another objects or for other purpose.
In java the objects which system can identify as "no longer in use" can be marked as garbage and later recycled. It is also called memory recycling.
Most of the object are not used after sometime only few has references further in the program. So java identify those objects which are no longer in use and recycles the memory.
So it helps the programmer to relieve the burden of memory management and hence increase the productivity.
Garbage collection process mainly handles memory management process, it does not take care about network connection or database connection or any user windows. Those has to be manually managed by programmer.
If any of the object is associated with other memory space and before the object is garbage collected and we want to ensure that the associated memory space should also be garbage collected then we can use the finalize method of object.
So in finalize method we could free if there is any non-java resource like file handle.
There is no way we can manually run Garbage Collector in Java
How It works : It finds the object which do not have any current strong reference ( to learn more about reference types you can read from link) or the one which is not any more reachable by any thread.
It also find the objects which has cyclic dependency i.e. the objects are called by each other and not referred anywhere else, if those objects are also not referred by any live thread then all those objects become eligible for GC
Generally, if any of the below points are satisfied, the object is eligible for GC in java:
1) There is no live reference to the object or all references of that object has been explicitly set as null
for ex. objectDog=null;
2) Another case is , when object is created inside a block and reference is not in scope after the control exits the block.
For example, objectDog was created inside the block and when the control goes out of block then objectDog become eligible for GC
{
ClassDog objectDog= new ClassDog();
..
..
..
}
3) The Holding object is set to null or say parent object is set to null , in case any object A is holding the references of another object B and the reference of object A i.e. the container object is set to null
then any child object or contained object in the holder object becomes eligible for GC
4) In case object is not having any strong reference or say the object has only weak reference then it would be eligible for GC
A weak reference is simply created like below :
WeakReference weakLink = new WeakReference(MyOwnMap);
Then in the code we can use weakLink.get() to get any object of MyOwnMap.
Because the weak references are not strong enough to hold GC so you may find after sometime that weakLink.get() has started returning null
Objects in java are created on Heap Space while the static members are created on java memory space
Both the heap and memory space area is shared by threads for using the resources
When JVM thinks that there is a need of memory based on the heap size, it collects all the object which are no longer in use or dont have any strong reference
If there is no memory space available in creating new object in heap, then it throws OutOfMemory(OOM) error
Manually we can not trigger GC process. However we can request GC by calling System.gc() and Runtime.gc() which is used to send request of GC to JVM but it’s not guaranteed GC will run that time.
Before removing any object from memory GC invokes finalize() method of that object in which we do the cleaning required before the object gets destroyed
In C/C++ or all the languages before java we used to manually free the unused memory by deleting the objects/reference created
so that system can use that memory for another objects or for other purpose.
In java the objects which system can identify as "no longer in use" can be marked as garbage and later recycled. It is also called memory recycling.
Most of the object are not used after sometime only few has references further in the program. So java identify those objects which are no longer in use and recycles the memory.
So it helps the programmer to relieve the burden of memory management and hence increase the productivity.
Garbage collection process mainly handles memory management process, it does not take care about network connection or database connection or any user windows. Those has to be manually managed by programmer.
If any of the object is associated with other memory space and before the object is garbage collected and we want to ensure that the associated memory space should also be garbage collected then we can use the finalize method of object.
So in finalize method we could free if there is any non-java resource like file handle.
There is no way we can manually run Garbage Collector in Java
How It works : It finds the object which do not have any current strong reference ( to learn more about reference types you can read from link) or the one which is not any more reachable by any thread.
It also find the objects which has cyclic dependency i.e. the objects are called by each other and not referred anywhere else, if those objects are also not referred by any live thread then all those objects become eligible for GC
Generally, if any of the below points are satisfied, the object is eligible for GC in java:
1) There is no live reference to the object or all references of that object has been explicitly set as null
for ex. objectDog=null;
2) Another case is , when object is created inside a block and reference is not in scope after the control exits the block.
For example, objectDog was created inside the block and when the control goes out of block then objectDog become eligible for GC
{
ClassDog objectDog= new ClassDog();
..
..
..
}
3) The Holding object is set to null or say parent object is set to null , in case any object A is holding the references of another object B and the reference of object A i.e. the container object is set to null
then any child object or contained object in the holder object becomes eligible for GC
4) In case object is not having any strong reference or say the object has only weak reference then it would be eligible for GC
A weak reference is simply created like below :
WeakReference weakLink = new WeakReference(MyOwnMap);
Then in the code we can use weakLink.get() to get any object of MyOwnMap.
Because the weak references are not strong enough to hold GC so you may find after sometime that weakLink.get() has started returning null
Objects in java are created on Heap Space while the static members are created on java memory space
Both the heap and memory space area is shared by threads for using the resources
When JVM thinks that there is a need of memory based on the heap size, it collects all the object which are no longer in use or dont have any strong reference
If there is no memory space available in creating new object in heap, then it throws OutOfMemory(OOM) error
Manually we can not trigger GC process. However we can request GC by calling System.gc() and Runtime.gc() which is used to send request of GC to JVM but it’s not guaranteed GC will run that time.
Before removing any object from memory GC invokes finalize() method of that object in which we do the cleaning required before the object gets destroyed
Comments
Post a Comment