In any production server using JVM performance can be improved by tuning the JVM parameters particularly those parameters which are related to memory and garbage collections.
We will try to learn about some JVm parameters which should be taken care of while going in production.
Heap size
The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap may be of a fixed size or may be expanded. The heap is created on virtual machine start-up. If you have complicated algorithms or big caching which might create lot of objects in memory you may need bigger heap size.
YOu can use the following java code to find out the heap size.It is in case of a simple java programme.
public class getHeapSize {
public static void main(String[]args){
//Get the jvm heap size.
long heapSize = Runtime.getRuntime().totalMemory();
//Print the jvm heap size.
System.out.println("Heap Size = " + heapSize);
}
}
The allocation of memory for the JVM is specified using -X options.
for example if you are using sun JVM the parameters are
-Xms initial java heap size
-Xmx maximum java heap size
-Xmn the size of the heap for the young generation.
In my previous posts i have discussed about JVM in learning java,however we need to go much deep in to the jvm in order to undestand the concept of jvm tuning.We will once again look in to the structure of JVM and try to find out what goes inside and how to fine tune it.
JVM as we have seen earlier executes the byte code and generates the output.JVM is a virtual computer that resides in a physical computer as a software process.However to generate any output jvm uses several components.The main components of JVM Are
the stack, the garbage-collected heap, the registers and the method area.we will check each of them one by one.
STACK-Stack in a Java virtual machine stores various method arguements as well as the local variables of any method.IT contains three registers which help in the manipulation of the stacks.Stack also keep track of each and every method invocation.
The three registers are
Vars register-All the local variables that are used during a method invocation are stored in Vars register.
Frame register-It is used to maintain the operation of stack itself.
Optop register-Here the parameters for bytecode instructions are placed, and results of bytecode instructions are found.
==================================
Method area-It is the actual aread where the byte code resides.The JVM has a program counter which always points to the next instruction and keep track of the current execution.
==================================
Garbage collection heap-The java programmes are stored here.when ever we create an object the memory is taken from this heap only.Any local object reference resides on Stack but the actual object resides in Heap only. Arays are also objects so they are also stored in garbage collection heap.
In java you can use the following scrip to find out the
Used Memory:
Free Memory:
Total Memory:
Max Memory:
public class Movie {
public static void main(String [] args) {
int mb = 1024*1024;
//Getting the runtime reference from system
Runtime runtime = Runtime.getRuntime();
System.out.println("##### Heap utilization statistics [MB] #####");
//Print used memory
System.out.println("Used Memory:"
+ (runtime.totalMemory() - runtime.freeMemory()) / mb);
//Print free memory
System.out.println("Free Memory:"
+ runtime.freeMemory() / mb);
//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory() / mb);
//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory() / mb);
}
}
Now we have some idea about the jvm we will dicuss about some jvm parameters and then we will check based on what criteria should we give values to them.
So depending upon your RAM size you have to assign values to the different parameters in your JVM.If your application runs on a single jvm or to make it more easy lets suppose your server uses a JVM and you want to assign it some values.If you have a ram og 2gb then you can have an initial heap size of 1gb.Please understand that there is no perfect settings for jvm.You have to do a lot of R &D in order to reach a correct setting.
As per sun recommendation the inital heap size and the maximum heap size must be the same. ie -Xms and -Xmx should be same.
The maximum heap size -Xmx controls how much memory the JVM can use. If your BPEL instance runs on a dedicated machine it can be set as high as the maximum memory space of your Operating System.
We have also talked about Eden space -Xmn.
It is the place where in short term process are stored as most of the process are short lived.So it should be 60-70% of the maximum heap size.
so having all these condition an ideal setting should be
-Xms2048m -Xmx2048m –Xmn1228m
This setting i have considered for a machine with 32 bit operating system linux box
The same settings can be applied in windows as 32 bit operating system in windows have a limitation for 2gb ,so if you don't have any other server or installation in your windows system then you can apply these changes.
If more than 2 cpu are getting used then one can go for aggressive heaping ,it is done by adding the following flag
-XX:+AggressiveHeap
This flag check the available memory and the number of processor associated with the machine automatically sets different properties for optimal performance.It works best in non windows environment.It does not have any effect on the performance of windows operating system.
So with all these setting the full configuration should be looking like
-Xms2048m -Xmx2048m –Xmn1228m -XX:+AggressiveHeap
No comments:
Post a Comment