0

I ran into this observation when I was trying to give a rough estimate of my code's runtime: The CPU usage stayed at around 10% for the first part of the code, which involved traversing 2 strings and tossing things into a hashset. When the code reached the second part (2 nested for loops, plus a gigantic array allocation), the CPU usage skyrocketed to 50%.

I don't think I did anything related to multithreading. Also if I slightly modify the first part (something really simple like adding another for loop), the CPU usage varies a lot too. Why is this happening? A bit curious about who's utilizing the CPU consumption.

4

2 回答 2

0

这不一定是您正在运行的代码和 CPU 使用率之间的直接比较,因为 JVM 在管理垃圾收集时会根据当时堆中的内容来波动 CPU 使用率。如果它突然上升到 50%,然后又回落,这可能是巧合,因为与此同时垃圾收集开始了。

于 2013-04-03T15:02:09.507 回答
0

您可以通过运行 jvisualvm.exe 来获取更多详细信息,该文件位于您的 jdk bin 文件夹中。我不确定有多少开箱即用的 CPU 使用信息,但至少有一些插件可以为您提供 CPU 使用信息。根据 david99world 的回答,这也是一种查看堆使用情况的图形方式。

http://visualvm.java.net/index.html

于 2013-04-03T15:12:30.240 回答