In Java Garbage collector is used internally to free the memory for JVM to work properly, and when memory is not enough for system or java application to work then exception is raised which belongs to java.lang package called OutOfMemoryError exception, If you are java developer I would doubt if you say this you never hit or get in life span. It usually comes when you not configure your content server who is managing your application or may be your application has memory leaks.
Now Below I will try to explain why memory leakage is raised in application.
Vector store = new Vector();
String GCnotCollected =new String(“TestString”);
Until you will not remove objects from vector with .remove(Added string) stated in above example Garbage Collector will not run or will not collect the object, which is to be called a memory leakage.
When memory created with creation of object reference and it is not released after usage – Memory Leakage
There are three main behaviors in java application when there is a memory leakage.
1. If your application is leaking memory overtime, eventually you are going to get java.lang.OutOfMemoryError exception.
2. Application heap usage will grow continuously( not application size)
3. Application will terminate automatically.
Now, What should we do to overcome this java.lang.OutOfMemoryError never come?
I would try to answer, There is a term called Java Heap size in Java, Java heap is the heap size allocated to JVM applications which takes care of the new objects being created. If the objects being created exceed the heap size, it will throw an error memoryOutofBound. Default heap size is 64 mb. It can be modified using command -Xms32m -Xmx512m (minimum 32mb maximum 512mb) but never make it too high so that all memory is used in creating the objects or not too low that no object is getting created this is required to be balanced, since there are other processes which also require memory for your application.
Java Code to get the JVM Memory Details
Runtime runtime = Runtime.getRuntime();long maxMemory = runtime.maxMemory(); // All Memory stated here is in Byteslong allocatedMemory = runtime.totalMemory();long freeMemory = runtime.freeMemory(); /*
* All Memory stated here is in Bytes, and may require to be change to Mega Bytes**/ System.out.println(“Allocated memory: ” +(allocatedMemory / 1024)/1024+”MB”);
System.out.println(“Max memory: ” + (maxMemory/1024)/1024+”MB”);
System.out.println(“Total free memory: ” + ((freeMemory + (maxMemory – allocatedMemory)) / 1024)/1024+”MB”);
Output depend on how much memory you have allotted to JVM:
Allocated memory: 101MB
Max memory: 514MB
Total free memory: 410MB .
Note: There is no formula to calculate how much memory should be provide to JVM to get the application run smoothly since it may dependent on many reasons, It will be your gut feels and experience from previous projects to set this memory, As per my understanding below are the some criteria’s you can use to decide the JVM memory:
1.) How many Objects you expect in your application, There are lots of tools which can be used to find Approx Objects in your applications.
2.) How many users will be using the application.
3.) Most important, Your server Configurations, You can’t set all RAM
memory to JVM memory since it may cause lots of damage to your system(Nothing
will run on your server)
Conclusion:Memory Leaks can be solve in two ways as below:
1.) Increase the memory according to application needs.
2.) Debug the Memory Leaks to fix it.
If you like reading above please spare 5 second to provide feed back or
share it with your friends.