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.
Andrew T Finnell
source share