1

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?

1 Answer 1

3

It sounds like you're looking for the Java Virtual Machine Monitoring and Management API.

In particular, take a look at MemoryMXBean and ThreadMXBean.

Sign up to request clarification or add additional context in comments.

2 Comments

This will help, thanks! Can you please look at my updated question?
@Ivan: AFAIK there's no such metric as "heap used by thread X" (how would you attribute objects that are shared by several threads or passed between threads).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.