0

I'm interested is there a way to dynamically increase or shrink the memory allowed to Java to occupy? I know that this is usually done at application start time but it will be very cool to see this in production time.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Looks like you cant but there is workaround: http://stackoverflow.com/questions/2884260/java-increase-xmx-dynamically-at-runtime – Marc Sep 17 '13 at 08:56

2 Answers2

3

The JVM does this already. It adjusts the heap size based on usage and can return memory to the OS (though not always)

What you can't change is the maximum. The reason you can't change it is that it reserves this virtual memory on startup as a single continuous block meaning it cannot shrink or grow it.

Give the maximum is just that, I suggest setting to a high value where you would rather the JVm fail than allocate more memory. In this case it doesn't make sense to adjust this IMHO.

BTW I don't suggest setting the minimum size unless you know the work load and the usage of the application very well (and in the future) The minimum is usually set for benchmarks, a known load and version of the application, but that doesn't make a good idea for applications with varying loads and code which changes over time.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • What can I configure dynamically while the JVM is running? Maximum number of network connections maybe? – Peter Penzov Sep 17 '13 at 10:08
  • The maximum number of connections is determined by the OS. What would you like to configure dynamically and what would you like to happen when this maximum is reached? You can configure a maximum number of database connections with some connection pooling libraries, and a maximum number of threads for a thread pool. – Peter Lawrey Sep 17 '13 at 12:03
1

I think that is highly dependent on the JVM you are using. For the reference implementation (OpenJDK/HotSpot) from Oracle I think it is not possible, the initial values are fixed.

But there are other JVM implementations, e.g. I know IBM has an own JVM, which could behave differently in that aspect. I suggest to take a careful look into the JVM documentations.

Jan Henke
  • 875
  • 1
  • 15
  • 28