overall CPU usage and Memory(RAM) usage in percentage in linux/ubuntu

17,510

Solution 1

You can use top and/or vmstat from the procps package. Use vmstat -s to get the amount of RAM on your machine (optional), and then use the output of top to calculate the memory usage percentages.

%Cpu(s):  3.8 us,  2.8 sy,  0.4 ni, 92.0 id,  1.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 24679620 total,  1705524 free,  7735748 used, 15238348 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 16161296 avail Mem 

You can also do this for relatively short output:

watch '/usr/bin/top -b | head -4 | tail -2'

A shell pipe that calculates the current RAM usage periodically is

watch -n 5 "/usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf(\"used: %s   total: %s  => RAM Usage: %.1f%%\", \$F[7], \$F[3], 100*\$F[7]/\$F[3]) if /KiB Mem/'"

(CPU + Swap usages were filtered out here.)

This command prints every 5 seconds:

Every 5.0s: /usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf("u...  wb3: Wed Nov 21 13:51:49 2018

used: 8349560   total: 24667856  => RAM Usage: 33.8%

Solution 2

Please use one of the following:

$ free -t | awk 'NR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
Current Memory Utilization is : 14.6715

OR

$ free -t | awk 'FNR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
Current Memory Utilization is : 14.6703
Share:
17,510
Haresh Vidja
Author by

Haresh Vidja

I am a full-stack developer, with a long professional history of work with web and open-source technologies like NodeJS, PHP, Angular, Redis, Sphinx, ElasticSearch. I am very passionate about writing reusable and secure software and am constantly learning. My current involvement in write microservices and DevOps, CI/CD integration with Gitlab, Docker, manage EC2 instances in AWS and droplets in DigitalOcean. For remote job contact me [email protected]

Updated on June 04, 2022

Comments

  • Haresh Vidja
    Haresh Vidja almost 2 years

    I want to findout overall CPU usage and RAM usage in percentage, but i dint get success

    $ command for cpu usage
    4.85%
    
    $ command for memory usage
    15.15%
    

    OR

    $ command for cpu and mamory usage
    cpu: 4.85%
    mem: 15.15%
    

    How can I achieve this?