You are looking for the current memory usage of your running process? Have you tried the following?
Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
Pergunta
I have linux system in which I have a JVM and it is used by 3 java process.
/home/codesrc>java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
So, I have 3 process running using the same JRE. each of this process has its own memory defined at command line when instating like
java -Xms256m -Xmx1024m -cp $CLASSPATH <class name>
java -Xms256m -Xmx512m -cp $CLASSPATH <class name>
java -Xms256m -Xmx705m -cp $CLASSPATH <class name>
The memory on the linux system is 4GB. I am trying to find the heap usage separately for each of these process. Since I have no JDK on the system I am unable to use jmap, jconsole or jstack. I tried
Runtime.getRuntime().totalMemory()
but that does not give me process based and also the output for the above doesn't see right Do any one have an idea on how to check the heap usage when only using JRE, something equivalent to
jmap -heap <PID>
Solução
You are looking for the current memory usage of your running process? Have you tried the following?
Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
Outras dicas
Your best bet is to enable the JMX agent when you start the Java VM on your server and use a host with JConsole available to connect.
Alternatively you can use a local app which uses JMX to retrieve the details (see other question):
List memBeans = ManagementFactory.getMemoryPoolMXBeans();
for (Iterator i = memBeans.iterator(); i.hasNext(); ) {
MemoryPoolMXBean mpool = (MemoryPoolMXBean)i.next();
MemoryUsage usage = mpool.getUsage();
String name = mpool.getName();
float init = usage.getInit()/1000;
float used = usage.getUsed()/1000;
float committed = usage.getCommitted()/1000;
float max = usage.getMax()/1000;
float pctUsed = (used / max)*100;
float pctCommitted = (committed / max)*100;
}