tags:

views:

338

answers:

2

Is it possible to change/modify/adding VM parameters after the JVM is already loaded (running)? If so, how can I do it?

+1  A: 

For properties you'd set via the -D flag on the command line, you want System.setProperty. For example:

System.setProperty("propname", "hello world");

// ... later ...
String value = System.getProperty("propname");

Update:

You can't enable debugging dynamically, but you can enable debugging at startup but attach a debugger later. With the following, you can listen on port 12345 and start your program running right away (via suspend=n). Then you can attach a debugger if/when you need to, detach the debugger, attach again later, etc.

-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=12345

Of course, this hurts performance even when the debugger isn't attached, so it only works well in dev/test code, not production. For that, you want logging, e.g. log4j.

Harold L
What about -X non standard options? especially -Xdebug and some other debugging flags?
Guy
I added an update with debugging info.
Harold L
Thanks you Harold!
Guy
@Guy if this answers your question please mark it as such, to (1) thank the person that helped you and (2) prompt others that are interested in the answer to your question that this solution works. Thanks.
Waleed Al-Balooshi
+1  A: 

A short answer is that you cannot change VM parameters at runtime. The Runtime class does expose some options such max memory. The main parameters such as max memory should only be set by an admin type allowing management of resources when multiple JVMs co exist on a machine. Allowing one JVM to get greedy and ask for lots and lots more than it was allocated would kill this constraint.

mP
The java.lang.Runtime class can tell you what the maximum memory setting is, but it doesn't allow it to be changed.
tdavies
@tdaviesI know and i gave the reason why this value is immutable.
mP