Skip to content

Java Programming

Java Garbage Collection

What is garbage collection?

It deals with identifying and freeing the memory of java application that are not used for a long time. There are two type garbage collection techniques, namely explicit and implicit methods.

Implicit Garbage collection and Explicit Garbage collection

Java run time system can invoke the garbage collector automatically so that the user never need to bother about the memory space when creating an object. This is implicit Garbage Collection.

Explicit Garbage Collection means the user is doing the collection intentionally. This is achieved by using System. gc();

Steps in a garbage collection

1. garbage collector searches for objects that are referenced in code.

2. garbage collector finds the unreferenced code.

3. garbage collector frees unreferenced objects and add their memory space to heap.

Methods in System

1) collect()

Request to the garbage collector to reclaim the memory to heap. By using this method it not sure that any inaccessible memory is reclaimed

public static void Collect()

2)keepAlive()

This method is used to ensure the existence of an object. Using this method we can add a value reference counter so it is not reclaimed by the garbage collector, we have to pass the object name as the parameter

public static void KeepAlive(Object obj);

Different Garbage Collector

Reference counting

One of the simple methods is reference counting. This type of garbage collection is very slow. In this type of GC each object will have a reference counter that will hold the value of how many times it has been referred. When a reference to an object is made by attaching a handle to it then the counter value will be incremented by one every time the handle goes out from the object or set to null, that means the reference is no longer exists the counter value will decremented. Thus reference count is small but constant work happens during the entire life time of your program. The garbage collector move through the entire list of the object. When GC find an object with reference count zero or null, it releases the storage. The main problem with the Reference count is when the object has circular reference then the GC cant find an object with zero value. Finding the self reference object need an extra effort. This is why reference GC is not being used in JVM(Java Virtual Machine)

Copy garbage collector

>If you need faster GC it is not possible with the reference counting. The other GC is based on the idea that any non-dead object can access easily whether they are in stack or static storage. This chain must go through several object. Thus you can find all the live object. For each handle you find, you must trace in to the object that the handle points and then trace all the handles of that object they points. This will continue after moving through the entire web. Each object that you passes through must be alive. The problem with self referential groups can be avoided by this. In this collector type JVM use adaptive garbage collection method. What it does is locating the object according to the variant currently being used. Each live object that found is copied from the one heap to another discarding all the garbage. In new heap objects are packed end to end.

Problem with this technique is you need to keep twice the memory that you actually need. The second issue is in stable program, where the garbage will be very small. But still the copy collector copy all the memory from one to another.

Mark and Sweep

Mark and Sweep uses same technology as the Copy garbage collector but the main difference is that it will not move the live object. In this kind of GC when it finds a live object the object is marked by setting the flag. Only after the marking process is finished the sweep occurs. In sweep all dead objects(unmarked) are released and no copying happens.

Through Garbage collection techniques the memory can be effectively managed in a java application.

Julian is a professional content writer on Java Development and now working at Telious Technologies.

Article Source: http://EzineArticles.com/?expert=Julian_Jaic

Be Sociable, Share!
    The following two tabs change content below.

    Latest posts by Ray (see all)