Friday, March 30, 2012

Java memory tuning

You may have come in scenario sometime when you program our cede result in error java memory related error, like could not allocate memory on heap for java, These error may result in OutOfMemoryError exceptions or to a reduction in the performance of the Java application, here is the typical setting for java memory so that you can escape these errors.

There is and command line option for java, that defines the maximum size of memory to be allocated to JVM

-Xmx    :  this allows application to run with 70% of max memory uses

Lets see the list of Maximum possible heap size and what is recommended for the heap size specification, as this is just a recommendation not a rule you can decide accordingly.

image

 

Jvm memory structure

 

By running following pice of code in java you can know how much memory is currently allocated to jvm

public class ReadCurrentHeapSize {
public static void main(String[]args){

//Get the jvm heap size.
long sizeOfHeap = Runtime.getRuntime().totalMemory();

//Print the jvm heap size.
System.out.println("Heap Size = " + sizeOfHeap );
}
}


 

Terms related to command line memory optimization options :

-Xms      :   initial java heap size

-Xmx     :     maximum java heap size

-Xmn     :    the size of the heap for the young generation

-Xss        :    the stack size for each thread

 

So you can change the setting by issuing the following command :

 

java -Xms64m –Xmx512m            don't forget to put "m , M, g, G" etc otherwise it will be in bytes

 

You can do this by changing catalina.sh (in Linux/Unix) or installservice.bat (for Windows). Open the file and search for the line where the minimum and maximum heap size for the JVM are being assigned.

This line would look something like this:

JAVA_OPTS=-Xms128m -Xmx256m

or 

--JvmMs 128 --JvmMx 256

 

You can explore some of the jdk tool to play more freely with jvm and java. Here is and example such a tool that will help you to play around with java and jvm

jconsole     :  Jconsole is a JMX-compliant monitoring tool.  It uses the extensive JMX instrumentation of the Java virtual machine to provide information on performance and resource consumption of applications running on the Java platform.

For more detail----->   clidk here

jps                :   The jps tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system. The tool is limited to reporting information on JVMs for which it has the access permissions.

                      For more reading click here

jstack           :    prints Java stack traces of Java threads for a given Java process or core file or a remote debug server.

                       For more reading click here

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...