Previous | Home | Next |
An application running on a system computer uses some memory, which makes memory management a significant issue for any programming language. As Java is comparatively high-level language, the memory management in Java is automatic. To make it more efficient, we need to understand garbage collection, i.e. freeing memory from objects that are no longer in use.
Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees a programmer from having to keep track of when to free allocated memory, thus preventing many potential bugs and problems.
What is a Garbage Collector ?
When we create an object by instantiating a class, the object is put on the heap, that is, it uses some memory. A Java application creates and uses objects. After an object in memory has been used and is no longer needed, it is sensible to free memory from that object.The Garbage Collector in Java initiates the memory management by freeing up the memory from objects that are no longer referenced.
The importance of this is that you do not need to code the memory management into your application program, also you have no control over when the garbage collector runs. While coding in some application we can do following things in order to help the memory management through the garbage collector :
-
Firstly, make an object eligible for the garbage collection, as garbage collector will only free memory from an eligible object.
-
Also make a request for garbage collection by making a system call to the garbage collector via, System.gc();.
We can also invoke the gc() method by using an instance of the Runtime class that a running program always has. It is contained in java.lang package, we can get hold of this instance by calling the static method getRuntime() of the Runtime class. The following example will show how we can invoke gc() method via Runtime class and some other runtime tasks,
Example
Java Tutorialslass garc { public static void main(String args[]) { Runtime runtimeobject = Runtime.getRuntime(); // creating an object of Runtime class // this statement returns the total memory available in bytes to jvm System.out.println( "The value of total memory of jvm is :" +runtimeobject.totalMemory()); // this statement returns the free memory available in bytes to jvm System.out.println( "The value of free memory availabe to jvm is :" +runtimeobject.freeMemory()); runtimeobject.gc(); // calling gc method on Runtime object System.out.println( "The amount of memory allocated to jvm after calling gc is :" +runtimeobject.freeMemory()); } }
It should be noted that a call to the garbage collector gives no guarantee that the memory will be free. There can be a situation that the JVM in which your program is running did not even implement the gc() method. The Java language also allows a dummy gc() method, the basic requirement for garbage collection is that you must make your object eligible for garbage collection. An object is considered eligible for garbage collection if there is no reference pointing to it. We can remove the references to an object in two ways:
-
By setting the object reference variable pointing to the object to null, such as,
GCclass object = new GCclass(); object = null;
-
Secondly by reassign a reference variable of an object to another object i.e. if a reference variable object is pointing to an object of the GCclass, you can free this object from this reference by pointing the reference to another object by the following statement
myObject = new GC2class();
Now, the object reference GCobject is pointing to an object of the class GC2class and not to an object of GCclass, to which it was pointing earlier.
Previous | Home | Next |