Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:22:25+00:00 2026-05-31T02:22:25+00:00

hi i am trying to get cpu load but i am getting per value

  • 0

hi i am trying to get cpu load but i am getting per value more then 100% not understanding what wrong i am doing .. .

as i am using core i3 and having two Virtual machines on it and on one VM i am doing this task my source code is,

public class PerformanceMonitor {

    static long lastSystemTime      = 0;
        static long lastProcessCpuTime  = 0;
        public static int  availableProcessors = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors();
      // public static int  availableProcessors = Runtime.getRuntime().availableProcessors();
        public synchronized double getCpuUsage()
        {
            ManagementFactory.getThreadMXBean().setThreadCpuTimeEnabled(true);
            if ( lastSystemTime == 0 )
            {
                baselineCounters();
              //  return ;
            }

            long systemTime     = System.nanoTime();
            long processCpuTime = 0;

            processCpuTime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
            double cpuUsage = (double) (processCpuTime - lastProcessCpuTime ) / ( systemTime - lastSystemTime )*100.0;

            lastSystemTime     = systemTime;
            lastProcessCpuTime = processCpuTime;

            return cpuUsage / availableProcessors;
        }

        private void baselineCounters()
        {
            lastSystemTime = System.nanoTime();

            lastProcessCpuTime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
        }



        }

and i am using this class in my code ,

  public static void main(String[] args) {

             monitor = new PerformanceMonitor();
            for(int i=0 ; i<10000 ; i++){
                start();
                double usage = monitor.getCpuUsage();
                if(usage!=0)System.out.println("Current CPU usage in pourcentage : "+usage);
            }
        }

        private static void start() {
            int count=0;
            for(int i=0 ; i<100000 ; i++){
                count=(int) Math.random()*100;
            }
        }
}

i am getting output like this,

Current CPU usage in pourcentage : 114.32926829268293
Current CPU usage in pourcentage : 48.70208931963182
Current CPU usage in pourcentage : 193.08746862328633
Current CPU usage in pourcentage : 236.85457129322594

hopes for your suggestions thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:22:26+00:00Added an answer on May 31, 2026 at 2:22 am

    I suspect the interval is too short. The cpu counters on linux only update 100 times per second and you could be performing the loop in about 1/100th of a second meaning you get a very large error. Try increasing the length of the loop so it is a few seconds long.


    I made the test longer and removed the / availableProcessors and I get numbers like this

    Current CPU usage in percentage : 99.95763231535723
    Current CPU usage in percentage : 99.94895863534565
    Current CPU usage in percentage : 99.40642907762789
    Current CPU usage in percentage : 99.96184668521532
    Current CPU usage in percentage : 99.89017532820115
    Current CPU usage in percentage : 99.91157680329947
    Current CPU usage in percentage : 99.96919753553432
    Current CPU usage in percentage : 99.94121334997978
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to get cpu load but I want to get just
Trying to get an ASP application deployed; it worked for a while but then
I'm Trying go GET CPU values using WMI classes but with no lucky. With
Trying to get a value out of 2d array inside of a hash and
I am trying to get total cpu usage in %. First I should start
Well I'm having a hell of a time trying to get my CPU down
I'm trying to start emulator-arm but it seems to get stuck and never shows
I trying to get started with Google Perf Tools to profile some CPU intensive
I'm trying to get the CPU serial or motherboard serial using C or Python
I am trying to get the CPU usage and details of all the running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.