Overview
Memory Management is a very crucial element in any types of applications. Java’s garbage collector provides an automatic solution to memory management.
The java objects are live on heap which is a part of memory and only part of memory that involved in garbage collection process.
When Garbage Collector runs ?
The garbage collector is under the control of JVM. JVM will run the garbage collector when it senses that memory is running low. You can ask the JVM to run the garbage collector but there is no guarantee that it will grant your request.
How Garbage Collector works ?
When garbage collector runs, its purpose is to find and delete the objects that cannot be reached.
The object becomes eligible for garbage collection when it is not reachable by any live threads.
In our java program has a reference variable that refers to an object as shown below and that reference variable is available to live threads then object is reachable.
Ex : Dog d = new Dog();
When we say new on Dog class, Dog object gets created on the heap and it is referred by reference variable d.
When reference variable die or no more in the scope, the object becomes an eligible for garbage collection.
Garbage Collection cannot be forced, we can just request to JVM to run garbage collection to free the memory. The simplest way to ask for garbage collection is
System.gc();
No guarantee that JVM will free the memory.
Explicitly making objects eligible for Garbage Collection
1) Assigning a null to a reference variable
An object becomes eligible for garbage collection when there are no reachable references to it. To remove a reference to an object is to set the reference variable to null.
Ex:
1 2 3 4 5 6 7 |
public class CollectGarbage{public static void main(String []args) { StringBuffer sb = new StringBuffer(“Good”); System.out.println(sb); //StringBuffer object is not eligible for garbage collection sb=null; // StringBuffer object becomes eligible for garbage collection } } |
2) Objects of Islands
A class that has an instance variable that is a reference variable to another instance of the same class. If all other references to these two objects are removed, even though they have valid reference to each other but no live thread will have access to either of the objects. When garbage collector runs it discovers such islands of objects and removes them.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Dog{Dog d; public static void main(String[] args) { Dog d1 = new Dog(); Dog d2 = new Dog(); d1.d = d2; d2.d = d1; d1=null; d2=null; } } |
The finalize() method
Sometimes an object will need to perform certain tasks before it is getting destroyed. Ex: If an object is holding some non java resource like file, We have to make sure that these resources are freezed before the object is getting destroyed.
To handle this kind of situations java provide a mechanism called finalize, by which we can define the action that has to be performed by garbage collector .
Every class inherits the finalize method from java.lang.Object.
finalize method is called by garbage collector when there is no more reference to the object exist.
finalize method will never run more than once on any object.
The method signature of the finalize method:
protected void finalize()
{
// Action has to be performed.
}
Setting Heap size
JVM will be running with fixed size of memory. If it finds no much space is available, it tries to run garbage collector to free the space to run application, even that much space is not available on heap it will throw java.lang.OutOfMemoryError, runtime exception. To resolve this you need to increase size of heap space based on your application.
Setting minimum heap size : java -Xms 256m className
Setting maximum heap size : java -Xmx 512m className
How to get the Heap size at run time ?
1 2 3 4 5 6 7 8 9 10 |
public class HeapSize {public static void main(String[] args) { int mb = 1024*1024; System.out.println("Used Memory:"+ (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/mb); System.out.println("Free Memory:"+ Runtime.getRuntime().freeMemory() / mb); System.out.println("Total Memory:" + Runtime.getRuntime().totalMemory() / mb); System.out.println("Max Memory:" + Runtime.getRuntime().maxMemory() / mb); } } |
Output :
Used Memory:0
Free Memory:4
Total Memory:4
Max Memory:63
Leave a Reply