64

Is there a way to set heap size from a running Java program?

trincot
  • 317,000
  • 35
  • 244
  • 286
feiroox
  • 3,069
  • 8
  • 31
  • 31

7 Answers7

65

No.

What you can do with an app that has very variable heap requirements is to set your max heap size very high with -Xmx and tune -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio so that the app will not hang on to a lot of memory when the heap shrinks (it does that with default settings).

But note that this may cause performance problems when the memory actually used by the app varies both strongly and quickly - in that case you're better off having it hang on to all the memory rather than give it back to the OS only to claim it again a second later. You might also want to fiddle with the GC options to ensure that the GC doesn't leave too much unclaimed objects lying around, which it tends to do when there's a lot of room for the heap to grow, and which would defeat the goal of wanting the heap size to adjust to the app's needs.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • -XX:MaxHeapFreeRatio The default value for this is 70. The free ratio is the amount of space not allocated on the heap over the total heap size. It the percentage of free space rises above the default of 70% the jvm will rreduce the size of the heap to allow the OS to use the memory. – 027 May 11 '18 at 08:15
  • This is possible (and I did it a lot), but the problem is that memory use can jump up and down a lot. It's not proportional to the actual live object set, just making a lot of objects in a row can cause the heap to explode. It only goes down if you manually cause a GC. So you basically have to reserve the maximum amount in actual RAM. We really need an option to change the maximum heap size dynamically. – Stefan Reich Jun 14 '18 at 22:15
13

According to http://www.dreamincode.net/forums/showtopic96263.htm, you can't do this at runtime, but you can spawn another process with a different heap size.

thSoft
  • 21,755
  • 5
  • 88
  • 103
  • Also known as a monumental pain in the backside. If you're using a lot of memory, the last thing you want to to is release it all, then reload it in another process. Of course, you can't keep the old process open long enough to transfer data as you're hogging more memory with 2 processes just to do the transfer. Better hope you guessed right _this_ time or you get to do it all over again. – Basic Jun 21 '15 at 20:48
7

You can tweak those settings when you start your application but once the JVM is up and running those values cannot be changed. Something like this:

java -Xms32m -Xmx512m FooBar

will set the minimum heap size to 32MB and the maximum heap size to 512MB. Once these are set, you cannot change them within the running program.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
2

The consensus may indeed be that this is not possible, but we should be looking at the JVM source to see how it can be ergonomically controlled. It would be very nice to be able to have a JVMTI agent be able to adjust the heap/perm/tenured/new/&c sizing online/at runtime.

What would this do? it would allow agents to infer sizing adjustments based upon performance or footprint goals which will be important when moving JVMs into the Cloud.

Jé Queue
  • 10,359
  • 13
  • 53
  • 61
1

You can use the -mx option on startup (also known as -Xmx) This is maximum size you should ever need in which cause you shouldn't need to set it to more than the maximum size you will ever need.

However, a work around is to have the main() check the maximum size and restart the java if the maximum size is not as desired. i.e. start another java program and die.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • This assumes you know in advance how much memory you'll need. That's fine for webservers/other things that stream but if you're analysing data (say building an index / looking for trends) you need to know up front what your data's going to be, which is pretty limiting – Basic Jun 21 '15 at 20:50
  • @Basic You don't need to know how much memory you will need. The -Xmx is the maximum teh JVM can have i.e. the program should crash with an OutOfMemoryError rather than use more memory. This maximum is usually based on how much memory you have and the default is 1/4 of main memory. The JVM won't use lots of memory for a "hello world" program regardless of what you set the maximum to. – Peter Lawrey Jun 22 '15 at 18:22
  • 1
    Assume you have a process and give it 50 gig of memory in advance and during running notice a lot of GC is going on and that probably 56 GiB would be better and you can not easily restart the program without doing a lot of recomputation... – kap May 08 '17 at 10:05
  • @kap so it is better to set the maximum to be a level at which you would rather the program fail than continue running. – Peter Lawrey May 08 '17 at 13:43
0

I asked same question to myself. And unlike answers above there is something I can do about my app increasing max heap JVM size. If app is a web server in cluster mode, I could start a new instance with changed min/max heap size and than shutdown initial instance. That shall be especially simple in GlassFish where you have management instance separated from nodeAgent(app server clustered instance) JVM.

Since many JVM applications are web apps, I think it worth to preserve in this blog.

Sasha Firsov
  • 699
  • 8
  • 9
-2

If I understand your question correctly, you're trying to change the heap size at runtime. I don't see any reason why this should be possible. Set the heap size at startup using the -Xmx JVM option. I also advise you to set the -Xms option only if you absolutely need to. This option sets the initial amount of head memory that is allocated for the JVM.

You should know how your application behaves in terms of memory. Set the the value of -Xmx wisely. If your app is some kind of a server app you can set a higher value, otherwise compromise your choice with other possible apps running in client machines and of course available memory.

bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • 2
    Set the value wisely... Yea, this is good advice. But consider this case - you are watching how you prod application getting close to the heap maximum, and you can do nothing with it. Yes there is a leak in the code... but first we need to keep alive during the day and then we will fix the problem – alsor.net Dec 18 '09 at 12:00
  • Note also that this might be an applet or other type of app where you do not control the VM startup settings. Those are cases where this would be nice. The main issue, I think, is that if you were able to change this you could run into security related memory issues. – Alex Vaz Feb 21 '13 at 00:41