开发者

Measuring memory and CPU used by a Java console application with java.lang.management

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 MemoryMXBean and ThreadMXBean.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜