I have a java program that performs 5 different tasks. When I run the program with -Xmx512m memory parameter, tasks 1-4 run fine but task 5 goes out of memory. When I run the program with -Xmx1024m, all 5 tasks runs fine but tasks 1-4 that previously ran fine with 512m heap now uses up almost all of 1024m heap. The same thing happens if I use -Xms128m -Xmx1024m.
What would be the memory parameters to instruct JVM to keep the memory utilization low (e.g. 512m for tasks 1-4) and only to use more memory when actually needed (e.g. in case of task 5)?
Maybe I need a way to activate the garbage collector more frequently than the default setting?
Set the Heap Size The most obvious place to start tuning the memory footprint is the Java heap size. If you reduce the Java heap size by a certain amount you will reduce the memory footprint of the Java process by the same amount. You can however not reduce the Java heap size infinitely.
In a Java program, the memory footprint is predominantly made up of the runtime environment in the form of Java virtual machine (JVM) itself that is loaded indirectly when a Java application launches.
The short answer is that you use these java command-line parameters to help control the RAM use of application: Use -Xmx to specify the maximum heap size. Use -Xms to specify the initial Java heap size. Use -Xss to set the Java thread stack size.
This is because the JVM steadily increases heap usage percentage until the garbage collection process frees up memory again. High heap usage occurs when the garbage collection process cannot keep up. An indicator of high heap usage is when the garbage collection is incapable of reducing the heap usage to around 30%.
These two parameters suggest the jvm when it needs to adjust its heap size:
-XX:MaxHeapFreeRatio=10
-XX:MinHeapFreeRatio=10
which pretty much means that if after gc, more than 10% of the heap is free, it will try to give the memory back to the OS. and it will grow the heap only if more than 90% of the heap is used after gc.
this might slow down your program, because the gc will be constantly changing allocated memory size. and it is possible that it will have no effect at all if your program allocates large amount of memory in a short period of time.
I think you have a misunderstanding on how the JVM works. This is not a GC issue or a "task" issue.
Your tasks have memory leaks or they are designed to hold onto more and more memory.
-Xmx1024m sets the maximum memory the JVM can allocate. It'd be the same thing as if you only have 1024 megs of physical memory and no virtual memory.
It would be helpful to update your question with the definition of Task. Are these 5 separate JVM's? Or just 5 units of work in a single JVM.
Update
I don't intend the program to use all 1g heap always. My intention is to instruct the JVM to use 512m heap if can manage and to use more memory only if required. When the memory is no more required, to fall back to 512m or even less amount of memory.
Just because you set -Xmx1024m does not mean the JVM WILL use all that memory. It's just a max limit. Setting Xms till set a minimum amount of memory to be used. Your program ultimately determines the running amount of memory being utilized. If it hits the limit set by -Xmx then it will throw an OutOfMemoryError
You can suggest to the JVM to run the Garbage Collector by invoking System.gc()
. Notice I said suggest, you cannot force the GC to run. You could be running on a platform that refuses to even do GC. You also need to look into what GC algorithm it is choosing for your application. I would look here Tuning Garbage Collector.
If you need such fine grain controls over memory usage you will need to pick something else besides the JVM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With