简体   繁体   中英

How to Instruct JVM to keep memory footprint as low as possible?

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 (eg 512m for tasks 1-4) and only to use more memory when actually needed (eg in case of task 5)?

Maybe I need a way to activate the garbage collector more frequently than the default setting?

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.

By passing -Xmx1024m to th VM you basically tell the VM that it can use up to 1 gig of heap, which it will eventually do. The VM starts to run garbage collection when it is needed, eg. when heap becomes used up, which depends on the available heap size. If you know which task to run outside (which I guess you do because you pass it as cmd line parameter), why not set the -Xmx from outside too?

try to use http://visualvm.java.net/ in order to see memory consumption. Then you may use jhat to visualize heap content and http://www.eclipse.org/mat/ to identify leaks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM