4

Possible Duplicate:
Read Java JVM startup parameters (eg -Xmx)

I'm running an application on a Tomcat server, and I want to access the values of the -Xms -Xmx and -XX:MaxPermSize JVM argument values so that I can log them for help with debugging.

I've tried to find these arguments listed in System.properties, and using System.getEnv(), but they are not there. Is there another way to get them, or, alternately, is their lack of presence in System.properties and System.getEnv() an indication that these options have not been set?

Community
  • 1
  • 1
Spike Williams
  • 35,795
  • 13
  • 48
  • 60
  • That's a helpful solution, although the Runtime.totalMemory() idea listed below is a good supplement to it, because it is helpful in the case where -Xms is not set, but you still want to see how much memory is there by default (as turned out to be the case with my issue). – Spike Williams Dec 19 '11 at 17:40
  • Why do it programmatically? It's set in the Tomcat configuration. Just copy that on every startup. – user207421 Dec 19 '11 at 23:22
  • Because I do not control the servers, and do not have access to the Tomcat configuration without having to track down and bother system administrators. – Spike Williams Dec 20 '11 at 16:23

2 Answers2

4

What about Runtime.totalMemory() (of course if you have permissions, or security is disabled) ?

Another workaround is to search for the command line that has started the tomcat and parse it. In linux is easy, but in windows...

Valchev
  • 1,490
  • 14
  • 17
4

You can get basic information from the Runtime class, including total, maximum and free memory; refer to Runtime JavaDoc for specifics.

If that isn't enough info, you can use the Management extensions to get a lot more information about current and peak memory usage in each of the memory pools. You can browse all of the available information for a running JVM using the MBeans tab on JConsole, which is included with the Oracle JDK. Specifically, look at the MBeans with object name java.lang:type=Memory, and java.lang:type=MemoryPool,name=*.

Once you've identified the MBeans with the information you need, you can access them programatically using the ManagementFactory class. This allows generic access to all MBeans (you can make your own), but has convenience methods for some of the MBeans provided by the platform, including:

Wesley Hartford
  • 426
  • 4
  • 6