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.
Can I change JVM parameters during execution [duplicate]
-
14-06-2023 - |
Frage
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?
Lösung
Andere Tipps
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.