+ Reply to Thread
Results 1 to 2 of 2

Thread: Get CPU/RAM usage

  1. #1

    Get CPU/RAM usage

    I am creating a monitor activity and i need to monitor the CPU and RAM usage so that i can display it on the notification bar , nice idea huh ?

    Can you help me doing that ?

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    41
    GET CPU USAGE :
    Code:
    private float readUsage() {
        try {
            RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
            String load = reader.readLine();
    
            String[] toks = load.split(" ");
    
            long idle1 = Long.parseLong(toks[5]);
            long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
                  + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
    
            try {
                Thread.sleep(360);
            } catch (Exception e) {}
    
            reader.seek(0);
            load = reader.readLine();
            reader.close();
    
            toks = load.split(" ");
    
            long idle2 = Long.parseLong(toks[5]);
            long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
                + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
    
            return (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));
    
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    
        return 0;
    }
    GET RAM USAGE :
    Code:
    MemoryInfo mi = new MemoryInfo();
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);
    long availableMegs = mi.availMem / 1048576L;

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts