How to instruct the JVM to take up as little memory as possible?

I have a Java program that performs 5 different tasks. When I run the program with the memory parameter -Xmx512m, tasks 1-4 work fine, but task 5 goes out of memory. When I run the program with -Xmx1024m, all 5 tasks run fine, but tasks 1-4, which previously worked fine, with a bunch of 512 m now use almost the whole bunch of 1024 m. The same thing happens if I use -Xms128m -Xmx1024m.

What will be the memory parameters to instruct the JVM to reduce memory load (for example, 512 m for tasks 1-4) and only to use more memory when it is really needed (for example, in case of task 5)?

Perhaps I need a way to activate the garbage collector more often than the default setting?

+8
java jvm
source share
4 answers

I think you have a misunderstanding about how the JVM works. This is not a problem with the GC or a problem with the task.

Your tasks have memory leaks or are designed to store more memory.

-Xmx1024m sets the maximum memory that the JVM can allocate. This would be the same as if you had only 1024 megabytes of physical memory and no virtual memory.

It would be helpful to update your question with the definition of the Problem. Are these 5 separate JVMs? Or just 5 units of work in one JVM.

Update

I do not want the program to always use the whole bunch of 1g. My intention is to instruct the JVM to use a heap of 512 m if it can manage and use more memory only if necessary. When memory is no longer required to return to 512 m or even less memory.

Just because you install -Xmx1024m does not mean that the JVM will use all this memory. This is just the maximum limit. Install Xms before setting the minimum amount of memory to use. Your program ultimately determines the amount of memory used. If it reaches the limit set by -Xmx then it will throw OutOfMemoryError

You can suggest the JVM to run the garbage collector by calling System.gc() . Notice, I said, I suggest, you cannot get the GC to work. You can work on a platform that refuses to even make a GC. You also need to learn which GC algorithm it chooses for your application. I would look here Tuning Garbage Collector .

If you need such small grain-based memory controls, you will need to choose something else besides the JVM.

+5
source share

These two parameters point to jvm when you need to adjust the heap size:

 -XX:MaxHeapFreeRatio=10 -XX:MinHeapFreeRatio=10 

which largely means that if after gc more than 10% of the heap is free, it will try to return the OS memory. and it will only increase the heap if more than 90% of the heap is used after gc.

this can slow down your program, since gc will constantly change the size of the allocated memory. and it is possible that it will not have any effect if your program allocates a large amount of memory in a short period of time.

+8
source share

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

+1
source share

try using http://visualvm.java.net/ to see memory consumption. You can then use jhat to visualize the contents of the heap and http://www.eclipse.org/mat/ to detect leaks.

+1
source share

All Articles