gpt4 book ai didi

java - 测量 JVM : java code 的 CPU 使用率

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:11 24 4
gpt4 key购买 nike

有没有办法跨平台(windows + unix + mac)测量 JVM 的 CPU 使用率(一旦 java 应用程序启动)?我使用过 Jconsole,但我需要的是执行此操作的 Java 代码,而不是可以监控 CPU 使用率的工具。我试过了

ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()

使用 JMX,但这无济于事,因为我需要的是 JVM 的特定 CPU 使用率(比如当我启动服务器时),而不是系统平均负载。

最佳答案

你应该看看 ThreadMXBean.getThreadCPUTime()来自 Thread MBean 的方法。

Thread CPU time

A Java virtual machineimplementation may support measuringthe CPU time for the current thread,for any thread, or for no threads.

还有 JTop 示例应用程序,它是 JDK jdk\demo\management\JTop\src\JTop.javahere 的一部分。 .看看:

/**
* Get the thread list with CPU consumption and the ThreadInfo for each thread
* sorted by the CPU time.
*/
private List<Map.Entry<Long, ThreadInfo>> getThreadList()

关于java - 测量 JVM : java code 的 CPU 使用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com