I am writing a Java program and at some point during execution I want to change the JVM settings (decrease the heap) and after a while increase it again. Is it possible with Java?
Asked
Active
Viewed 205 times
4
-
Can I ask _why_ you'd want to do that? – DaveH Mar 14 '14 at 10:36
2 Answers
1
Long story short: you can't. Heap size is fixed once you are running it, and there's no way to modify it from the code.

Silverlord
- 88
- 7
1
I do not think this is possible, but you could of course control the heap with -Xmx or -Xms. You can also play with : -XX:MaxHeapFreeRatio : this is the maximum percentage (70 by default if I am no mistaken) of the heap that is free before GC will shrink it.
Giving a small -Xms will make the heap grow (if needed and will involve a full GC) and shrink back is possible also.
Generally people try to avoid as much as possible this shrinking and growing because it involves a gull GC aka stop-the-world events that will slow you down.

Eugene
- 117,005
- 15
- 201
- 306