Garbage Collector in Java:-
In my last post in Heap Concept I have told how JVM allocates memory for program. Now say JVM loads a program into main memory & program ended with success. For us it’s over. But for Java there are tasks to do, whenever a program is over, we need to remove it’s source code for next program to execute. All the structures & unused referenced need to be deleted from memory for your next code to run successfully. These unused or used objects ,references are called garbage and they need to be removed. The removal process is called Garbage collection .
Disadvantages of Manual Garbage collection:
In some of the programming language it is a programmer’s responsibility to free up the memory. The complexity of that task leads to many common errors that can cause unexpected or erroneous program behavior and crashes. As a result, a large proportion of developer time is often spent debugging and trying to correct such errors.
while developer tries to free up memory space there might be situation occrures when they deallocate the space used by an object to which some other object still has a reference. If the object with that (dangling) reference tries to access the original object, but the space has been reallocated to a new object. Result is unpredictable situation. It is called dangling references
There might be another situation when developer might have made a mistake to do a proper coding which might call the space leaks. These leaks occur when memory is allocated and no longer referenced but is not released. If enough leaks occur, they can keep consuming memory until all available memory is exhausted.
Automatic Memory Management :
So there was a improvement area over a manual garbage collection and an automatic memory collection. Java being an advanced language , has came up with the automatic memory management.Java does not allow users to de-allocate the memory rather it executes a program called Garbage Collector for memory management.The job of the garbage collector to free up the memory.
Dynamic allocation of memory for an object is achieved by using the new Operator.Using new operator when an object is created some memory is allocated to that object in the memory section.The memory remains allocated till the time there are some reference present for that object to use it. When there is no reference present, the memory is reclaimed by java.
What primary goal for automatic memory management–
Programs which can not deallocate the memory can crash the application when there is no memory left to allocate.These programs are said to have memory leak issues
Automatic Garbage collection avoids the dangling reference problem, because an object that is still referenced somewhere will never be garbage collected and so will not be considered free. Garbage collection also solves the space leak problem described above since it automatically frees all memory no longer referenced.
This task is done by garbage collector in Java.Java implemented this using a demon thread. This thread when called runs invoke method to do this activity.However we don’t have any control over this. Only JVM,if realizes ,it requites more space in memory to run current program, activates this method to clean up memory.
We can only send request JVM to clean up memory by
But it purely depends on Heap Size & requirement of space so that JVM activates the process. But there is no free () or delete () function in Java like c++; JVM performs garbage collection in a low priority thread.
How Garbage collector works:-
Java’s garbage collector checks for any active reference of any object/thread/static reference. If it found the specific object is not reachable from any live threads or any static reference it frees up the memory
Causes for Garbage Collection:-
• All object references that are assigned to null.
• The object is created inside a block & control goes out of the block.
• Say a object holds references of some other object & parent object is set to null.Then both the objects becomes dead & eligible for garbage collection.
• If an object has a live reference from the hashmap.
• All cyclic references (an object “A” points to another object “B” & object “B” again refer to object “A”) if dead is also eligible for garbage collection.
Garbage Collection Method:-
The popular algorithms for garbage collection are:
• Throughput Garbage Collector–
• Incremental low pause collector
• Referencing counting collector-This is bit old strategy. In this process when an object is instantiated the reference counter becomes one(1) and more the object is used/assigned reference by some other root or object JVM keeps incrementing its value.But when the object is set a new value or object becomes out of scope referencing counter is decremented by 1. By this method when referencing pointer becomes zero. This object becomes garbage collectable. This method is best when code size is small.but takes a long time when code size is very big and fails if there is any circular reference of object happens
• Tracing collector: This is a two-phase graph based algorithm. First-Mark and second is Sweep.By this algorithm every time JVM gets the root and try to navigate the branches of the objects. If it finds a suitable reference from root to any other object, the object is considered as alive and Garbage collector marks it , otherwise the object is not marked , treated as dead and in the sweep phase the object is removed. This algorithm is good but can not handle the fragmentation issue.
• Compacting Collectors:It is created on top of Tracing collector to minimize the problem of Heap fragmentation. In this process Mark is activated and post to that Sweep is activated. In the third phase Collector basically fragment the space i.e. move all the live objects aside and rectify the heap pointer.For this multiple assignment this algorithm is again good when there are less number of objects in the Heap memory but takes a large amount of time when objects are more.So in a word it is having performance issue.
1. Takes a very long time
2. Time spent is dependent on the size of the heap
3. That is why the “Permanent Space” was invented
4. And cause un/reloading of classes wasn’t very common either
• Copying Collector: In this approach the entire heap is divided into two halves…Old and new heap zone.Initially when a program starts ,JVM starts to assign area from new zone. after sometime when Garbage collector gets activated ,it stops the programme running, the live objects are placed to the other half . The new area is marked as New and the used area is marked as old and garbage collected. Post this operation the normal programme execution resumes. This approach solves all the memory related previous problems but introduce new kind of problem.How?? Every time JVM starts to execute the allocated memory for Heap is divided into two halves. So effective usage area is reduced. One survivor space is always empty.Serves as destination for minor collections .Objects get copied to the tenured space when the 2nd survivor space fills up.
Major collections occur when the tenured space fills up.Major collections free up Eden and both survivor space
• Stop active programming
• Incremental in nature
• Time GC with new object creation
• If GC runs, suspend new allocation Concurrent/Parallel
• Allocation happens at the same time as GC
• Very complex locking regimes
• Generations/Spaces make it easier
• Concurrent low pause or short pause collector- By this technique garbage collection and program run parallel. This underneath uses the graph based technique and rectify the heap size of copying collector. So reclaiming and fragmentation of heap memory happens at a time.Since most of the work of tracing the object graph is waiting for the computer’s memory system, the high throughput collector spins up multiple threads to try to hide that latency. The interesting part of the parallel collector is the way it uses multiple threads to copy objects, but still ends up with one large, unfragmanted block of free space when it is done.