Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting JVM parameters at runtime

Tags:

java

jvm

ikvm

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

like image 264
Guy Avatar asked Nov 18 '09 06:11

Guy


People also ask

Where should I put JVM parameter?

To add JVM parameters, choose New . The property is added to Custom parameters . The actual parameters used by a running JVM can be found in the development trace file of the corresponding server process. This results in parameter: "-agentlib:myagent=port=12345,dir=C:/Mydir" .

How do I change the default JVM argument?

-- Go to the Eclipse Window > preferences, in "Java > Installed JREs". -- Copy the current default JRE with a new name, for example myJRE. -- Select the new JRE and click on the "Edit" button. -- In the "Edit JRE" dialog, add your JVM arguments in the "Default VM Arguments" field.

What are the and parameters when starting JVM?

Now let's discuss the most frequently used JVM Parameters which are 3 namely as follows: Java Heap Size. Garbage Collector. Print GC.


2 Answers

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.

like image 188
Harold L Avatar answered Oct 12 '22 22:10

Harold L


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.

like image 31
mP. Avatar answered Oct 12 '22 20:10

mP.