File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Xmx Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Xmx" Watch "Xmx" New topic
Author

Xmx

Mohtashim Shaikh
Ranch Hand

Joined: Jul 18, 2010
Posts: 43
Is there a way to read the -Xmx that my JVM PID is running with whether it is explicitly set or default.
Prefer a unix command (reading -Xmx in the unix shell script), if not then java code.

Thank,
Mohtashim
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Do a


and then examine the resulting line. The last thing on the line is the full command used to start the program and include -Xmx etc if they were specified.

You might need to use the grep '-e' switch to specify a regular expression to more tightly control the matched lines but I leave that up to you.

If you don't actually know the PID then you can grep for the program jar name or class name instead (depends how you invoked it). Of course this will assume that you only have one copy running whereas specifying the PID and using a regex one can exactly match the program of interest.

You can go even further with the 'ps' command and extract just the fields of interest using the --format option. I can never remember the details so consult the 'man' page for 'ps' .

Edit : The information you need to work with can probably be obtained using



Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Mohtashim Shaikh
Ranch Hand

Joined: Jul 18, 2010
Posts: 43
Thanks for yr inputs, however I am still wanting to know how can we get the default -Xmxof a running JVM PID which does not have -Xmx set explicitly.

Regards,
Mohtashim
Rohan Dhapodkar
Greenhorn

Joined: Jun 27, 2011
Posts: 23
You can use java.lang.Runtime.maxMemory() which will return max memory size that is set using -Xmx.
Oracle Java 5 documentation says that based on your hardware architecture java determines whether it's server configuration or client configuration and then it has some rules to set -Xmx and -Xms

http://download.oracle.com/javase/6/docs/technotes/tools/windows/java.html
http://download.oracle.com/javase/6/docs/technotes/guides/vm/gc-ergonomics.html
http://download.oracle.com/javase/6/docs/technotes/guides/vm/server-class.html
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Rohan Dhapodkar wrote:You can use java.lang.Runtime.maxMemory() which will return max memory size that is set using -Xmx.l


It is my understanding that the OP only has the PID of the Java process so cannot get at any of the internal information. This is why I suggested parsing the output of the 'ps' command but obviously this fails if no -Xmx has been set.

One possibility is that if parsing the output of 'ps' does not produce a -Xmx value then the OP should assume the default value which must be documented somewhere. Of course this will only work if there is no way to set the -Xmx other than as an argument to 'java'.
Rohan Dhapodkar
Greenhorn

Joined: Jun 27, 2011
Posts: 23
If you really want to get those parameters of running JVM progrmatically, you can use JMX.
1. get your process id using jps or ps.
2. connect to running JVM . http://www.pongasoft.com/blog/yan/entry/connecting_to_a_local_vm/
3. get MemoryMxBean http://download.oracle.com/javase/7/docs/api/java/lang/management/ManagementFactory.html#getMemoryMXBean%28%29
4.then from MemoryMxBean you can get memory details . http://download.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html

or you can use jconsole, which does the same thing.
 
wood burning stoves
 
subject: Xmx