When people say JVM tuning, most of the time they mean tuning GC. Before we start to tune JVM for Liferay Portal, let us talk about some basic conceptions of GC.
As we all know jvm can handle useless memory block automaticly, this release us from manually free useless memory blocks which is the NO.1 burden for all C and C++ programmers. With the help from GC our life is much easier, we don't need to worry about memory leak for every single line of our code. But this does not mean you can ignored freeing memory.
So let me ask you a very common question: Manually Memory Management VS. Automatic Memory Management, which one is better?
The answer will change depending on how you define better, and who is making the definition.
Let us see the first part, how do you define better?
If easy is better, of course, Automatic Memory Management is better. You don't need to do anything explicit memory management, JVM takes care of everything.
If flexible is better, of course, Manually Memory Management is better. Everything is under your control.
For the second part, who is making the definition?
If you are C and C++ guru, of course, Manually Memory Management is better.
If you are not, most people whom are reading this blog should be this case:), Automatic Memory Management is better.
If you are not very good at Manually Memory Management, you will be very easy to make following mistakes:
Dangling references: It is possible to deallocate the space used by an object to which some other object still has a reference. If the object with that (dangling) reference tries to access the original object, but the space has been reallocated to a new object, the result is unpredictable and not what was intended.
Memory leak:These leaks occur when memory is allocated and no longer referenced but is not released. For example, if you intend to free the space utilized by a linked list but you make the mistake of just deallocating the first element of the list, the remaining list elements are no longer referenced but they go out of the program’s reach and can neither be used nor recovered. If enough leaks occur, they can keep consuming memory until all available memory is exhausted.
Another big problem for memory management is the memory fragments. With Manually Memory Management it is very difficult to fix this problem, but with Automatic Memory Management it becomes very easy.
Suppose you have 4MB heap and you allocate three objects which are 1MB, 1MB and 1MB, so your heap should looks like this:
Then you free the second object, so your heap should looks like this:
Finally you need to allocate the 4th object which is 1.5MB, at this point you have 2MB free heap space, but you can't fit a 1.5MB object into it.
The memory fragments can cause fake out of memory, and can also make memory allocating very slow. Because you will need a link list to record all the free blocks, and do a search for every allocate request.
Ok, that is enough. For more information about JVM memory management you can read memorymanagement_whitepaper
Let us start our real topic:Tune JVM for Liferay Portal.
1)Check your portal server's CPU and memory. Our portal server has 2 4-cores cpu, which means 8 cpus for JVM, and we have 8GB physical memory.
Because we have multiple cpus, we should try to use multi-thread to speed up gc.
In Sun JDK5, there are 4 build-in gc types: Serial Collector, Parallel Collector, Parallel Compacting Collector and Concurrent Mark-Sweep (CMS) Collector.
For our server, we choose Concurrent Mark-Sweep (CMS) Collector, because we have 8 cpus and we need a short gc pause time.(For more detail, please read memorymanagement_whitepaper). Use -XX:+UseConcMarkSweepGC to turn on this option.
2)Fix your Xms and Xmx to a same value. Because our server is a dedicated server for Liferay Portal, once you know the suitable heap size for the whole JVM, there is no reason to define a range then let jvm to group the heap size. We should directly set the heap size to the suitable value. Use -Xms2048m -Xmx2048m to set the heap size.(i will explain why is 2048 later.)
3)Set the young generation size(Don't know what is young generation? again read memorymanagement_whitepaper). For the same reason as above, we should fix the young generation size directly to the suitable size. Experimentally we make the young generation size 1/3 of the whole heap. Use -XX:NewSize=700m -XX:MaxNewSize=700m to set the young generation size.
4)Set PermSize. PermSpace is used to store Class Object, so the size depends on how many classes do you have. For our case 128MB is big enough. If you get a OutOfMemory error complain for there is no more space in PermSpace, you should increase this value. Use -XX:MaxPermSize=128m to set the PermSize.
5)Set the SurvivorRatio to young generation. There is no particular rule for this, the value is from watching(By VisualVM). You should try to make survivor size bigger than the peak memory use size. Here we set the ratio to 20. Use -XX:SurvivorRatio=20 to set the ratio.
Ok, now let us talk about why set the heap size to 2048MB, we are using 64bit JVM, we can support more than that heap size. The rule for choosing heap size is that fit is the best, never making it too big. The algorithm for gc tells us as the heap size increasing by a linear way, the time for gc will grow much more faster than linear. So we should just set the heap size to meet our need. In our case, by testing with VisualVM, we choose 2048MB for the heap size. From the gc time for young generation and old generation you can tell whether this heap size is suitable. Generally the average young generation gc time should around 20ms, the old generation gc time should around 200ms~400ms.
The finally startup parameters should look like this:
JAVA_OPTS="$JAVA_OPTS -XX:NewSize=700m -XX:MaxNewSize=700m -Xms2048m -Xmx2048m -XX:MaxPermSize=128m -XX:+UseConcMarkSweepGC -XX:SurvivorRatio=10"
For more info about jvm startup parameters, please read jvm-options-list

