Skip to main content

Garbage Collector

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

Comments

Popular posts from this blog

OBJECT class in Java

OBJECT class in Java : Object is at the top of class hierarchy in java. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class. Mainly below methods are provided by Object class : public String toString() returns the string representation of this object. protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object. public boolean equals(Object obj) compares the given object to this object. public int hashCode() returns the hashcode number for this object. public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of ...

Java Priority Queue sort using lambda expression

Priority Queue :     In Java  Priority Queue  is a  queue  which keeps its elements sorted as per their natural order( example in ascending orders for Integer, alphabetical a-z for alphabets) or using a custom Comparator at the time of creation you can custom sort it. It has most method  similar to a queue  add, clear, poll, peek As priority queue have to compare elements to keep in order so elements must be comparable otherwise, it will throw ClassCastException .  As null can not be compared so you are not allowed to insert null too A program to sort priority queue using Lambda function in Java           c lass   PriorityQueueLambdaJava { public List<Integer> KFrequent( int [] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for ( int j: nums) { map .put(j, map .getOrDefault(j, 0 ) + 1 ); } PriorityQueue<Map.Entry<Integ...

Important Keywords: Static, Continue,Break:

Important Keywords: Static, Continue,Break:  Static: The static keyword in java is used for memory management mainly.  The static keyword members belongs to the class rather than instance of the class. Following members can be declared as static: variable (also known as class variable) method (also known as class method) block nested class  static variables : We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class. Since static variables are shared across all the instances of Object, they are not thread safe . Usually static variables are used with final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it as: ClassName.variableName //static variable example private static int a ge; public static String name; static methods: A static method belongs t...