Java Garbage Collection

What is garbage collection?value will decremented. Thus reference count is small
It deals with identifying and freeing the memory ofbut constant work happens during the entire life time
java application that are not used for a long time.of your program. The garbage collector move
There are two type garbage collection techniques,through the entire list of the object. When GC find
namely explicit and implicit methods.an object with reference count zero or null, it
Implicit Garbage collection and Explicit Garbagereleases the storage. The main problem with the
collectionReference count is when the object has circular
Java run time system can invoke the garbagereference then the GC cant find an object with zero
collector automatically so that the user never needvalue. Finding the self reference object need an extra
to bother about the memory space when creating aneffort. This is why reference GC is not being used in
object. This is implicit Garbage Collection.JVM(Java Virtual Machine)
Explicit Garbage Collection means the user is doingCopy garbage collector
the collection intentionally. This is achieved by using>If you need faster GC it is not possible with the
System. gc();reference counting. The other GC is based on the
Steps in a garbage collectionidea that any non-dead object can access easily
1. garbage collector searches for objects that arewhether they are in stack or static storage. This
referenced in code.chain must go through several object. Thus you can
2. garbage collector finds the unreferenced code.find all the live object. For each handle you find, you
3. garbage collector frees unreferenced objects andmust trace in to the object that the handle points
add their memory space to heap.and then trace all the handles of that object they
Methods in Systempoints. This will continue after moving through the
1) collect()entire web. Each object that you passes through
Request to the garbage collector to reclaim themust be alive. The problem with self referential
memory to heap. By using this method it not suregroups can be avoided by this. In this collector type
that any inaccessible memory is reclaimedpublic staticJVM use adaptive garbage collection method. What it
void Collect()does is locating the object according to the variant
2)keepAlive()currently being used. Each live object that found is
This method is used to ensure the existence of ancopied from the one heap to another discarding all
object. Using this method we can add a valuethe garbage. In new heap objects are packed end to
reference counter so it is not reclaimed by theend.
garbage collector, we have to pass the object nameProblem with this technique is you need to keep
as the parameterpublic static void KeepAlive(Objecttwice the memory that you actually need. The
obj);second issue is in stable program, where the garbage
Different Garbage Collectorwill be very small. But still the copy collector copy all
Reference countingthe memory from one to another.
One of the simple methods is reference counting.Mark and Sweep
This type of garbage collection is very slow. In thisMark and Sweep uses same technology as the Copy
type of GC each object will have a referencegarbage collector but the main difference is that it will
counter that will hold the value of how many times itnot move the live object. In this kind of GC when it
has been referred. When a reference to an object isfinds a live object the object is marked by setting
made by attaching a handle to it then the counterthe flag. Only after the marking process is finished
value will be incremented by one every time thethe sweep occurs. In sweep all dead
handle goes out from the object or set to null, thatobjects(unmarked) are released and no copying
means the reference is no longer exists the counterhappens.