Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You <em>can</em> use jMX beans to calculate a CPU load. Note that this measures CPU load of your java program, not the overall system load. (the question didn't specify which)</p> <p>Initialize:</p> <pre><code> ThreadMXBean newBean = ManagementFactory.getThreadMXBean(); try { if (this.newBean.isThreadCpuTimeSupported()) this.newBean.setThreadCpuTimeEnabled(true); else throw new AccessControlException(""); } catch (AccessControlException e) { System.out.println("CPU Usage monitoring is not available!"); System.exit(0); } </code></pre> <p>Then as your loop (assuming your application uses a loop, otherwise what's the point in measuring CPU usage?) use this:</p> <pre><code> long lastTime = System.nanoTime(); long lastThreadTime = newBean.getCurrentThreadCpuTime(); while (true) { // Do something that takes at least 10ms (on windows) try { int j = 0; for (int i = 0; i &lt; 20000000; i++) j = (j + i) * j / 2; Thread.sleep(100); } catch (InterruptedException e) { } // Calculate coarse CPU usage: long time = System.nanoTime(); long threadTime = newBean.getCurrentThreadCpuTime(); double load = (threadTime - lastThreadTime) / (double)(time - lastTime); System.out.println((float)load); // For next iteration. lastTime = time; lastThreadTime = threadTime; } </code></pre> <p>You need to use double precision because a long doesn't fit in a float (though it might work 99.9999999999999999% of the time)</p> <blockquote> <p>If the 'something' you're doing takes less than approximately 1.6ms (Windows), then the returned value will not even have increased at all and you'll perpetually measure 0% CPU erroneously.</p> </blockquote> <p>Because <code>getCurrentThreadCpuTime</code> is VERY inaccurate (with delays less than 100ms), smoothing it helps a lot:</p> <pre><code> long lastTime = System.nanoTime(); long lastThreadTime = newBean.getCurrentThreadCpuTime(); float smoothLoad = 0; while (true) { // Do something that takes at least 10ms (on windows) try { int j = 0; for (int i = 0; i &lt; 2000000; i++) j = (j + i) * j / 2; Thread.sleep(10); } catch (InterruptedException e) { } // Calculate coarse CPU usage: long time = System.nanoTime(); long threadTime = newBean.getCurrentThreadCpuTime(); double load = (threadTime - lastThreadTime) / (double)(time - lastTime); // Smooth it. smoothLoad += (load - smoothLoad) * 0.1; // damping factor, lower means less responsive, 1 means no smoothing. System.out.println(smoothLoad); // For next iteration. lastTime = time; lastThreadTime = threadTime; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload