I need to measure how much memory and CPU my application is using at the moment, and I need to measure that from the very same application. Any advices how to do that? I’ve been using jconsole, but I can’t find an API which would enable me to use it from a console application.
Thanks.
EDIT: As the user aix recommended, I’m using java.lang.management to achieve my goal. However, I have few questions about it. This is the code I wrote:
MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
MemoryUsage memoryUsage = bean.getHeapMemoryUsage();
double used = (double)memoryUsage.getUsed() / (1024 * 1024); // in MB
double max = (double)memoryUsage.getMax() / (1024 * 1024); // in MB
I’m calling this from the application that I want to measure. What exactly does this measure? Memory taken by the whole application? By the current thread?
It sounds like you’re looking for the Java Virtual Machine Monitoring and Management API.
In particular, take a look at
MemoryMXBeanandThreadMXBean.