0

I have an application that can be executed when I use the jvm command -Xmx65m. Although it is running fine, I want to allow the application to consume more memory, because it has some features that require it. The problem is that if I increase the -Xmx option, the JVM will alocate more memory to run the features that it can handle with only 65 MB.

Is it possible to configure the JVM to only request more memory to the SO only when it is running out of options and it is about to throw an OutOfMemoryError?

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40
  • Can you explain how it is different to what the JVM does currently? – Peter Lawrey Oct 22 '12 at 11:28
  • *"Is it possible to configure the JVM to only request more memory to the SO only when it is running out of options and it is about to throw an OutOfMemoryError?"* Basically, no. But that is what the `Xms` option is for. – Andrew Thompson Oct 22 '12 at 11:28

3 Answers3

3

Please add the min , max memory settings. So that JVM can start with the min required memory and keeps allocating more memory as and when its required.

-Xms65m -Xmx512m

Hope this helps.

Subba
  • 396
  • 2
  • 13
2

The JVM reserves the maximum heap size as virtual memory on start up but it only uses the amount it needs (even if you set a minimum size it might not use that much) If it uses a large amount of memory but doesn't need it any more it can give back to the OS (but often doesn't AFAIK)

Perhaps you are not seeing a gradual increase as your maximum is so small. Try it with a maximum of -mx1g and look in jvisualvm as to how the maximum heap size grows.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Is it possible to configure the JVM to only request more memory to the SO only when it is running out of options and it is about to throw an OutOfMemoryError?

As the JVM gets close to finally running out of space, it runs the GC more and more frequently, and application throughput (in terms of useful work done per CPU second) falls dramatically. You don't want that happening if you can avoid it.

There is one GC tuning option that you could use to discourage the JVM from growing the heap. The -XX:MinHeapFreeRatio option sets the "minimum percentage of heap free after GC to avoid expansion". If you reduce this from the default value of 40% to (say) 20% the GC will be less eager to expand the heap.

The down-side is that if you reduce -XX:MinHeapFreeRatio, the JVM as a whole will spend a larger percentage of its time running the garbage collector. Go too far and the effect could possibly be quite severe. (Personally, I would not recommend changing this setting at all ... )

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216