5 commands to check memory usage on Linux

Source: Internet
Author: User


Memory Usage


On Linux, there is commands for almost everything, because the GUI might is always available. When working on servers only the shell access is available and everything have to be do from these commands. So today we shall is checking the commands that can is used to check memory usage on a Linux system. Memory include RAM and swap.



It is often important to check memory usage and memory used per process on servers so the resources do not fall short and Users is able to access the server. For example a website. If you were running a webserver, then the server must has enough memory to serve the visitors to the site. If not, the site would become very slow or even go off when there is a traffic spike, simply because memory would fall SH Ort. Its just-like-what-happens on your desktop PC.


1. Free command


The free command was the most simple and easy to use command to check memory usage on Linux. Here is a quick example


$ free -m
             total       used       free     shared    buffers     cached
Mem:          7976       6459       1517          0        865       2248
-/+ buffers/cache:       3344       4631
Swap:         1951          0       1951


The M option displays all data in MBs. The total OS 7976 MB are the total amount of RAM installed on the system, which is 8GB. The Used column shows the amount of RAM that have been used by Linux, in this case around 6.4 GB. The output is pretty self explanatory. The catch is the cached and buffers column. The second line tells this 4.6 GB is free. This was the free memory on first line added with the buffers and cached amount of memory.



Linux has the habit of caching lots of things for faster performance, so the memory can is freed and used if needed.
The last line was the swap memory, which in this case was lying entirely free.


2./proc/meminfo


The next-to-check memory usage is to read The/proc/meminfo file. Know that the/proc the file system does not contain real files. They is rather virtual files that contain dynamic information about the kernel and the system.


$ cat /proc/meminfo
MemTotal:        8167848 kB
MemFree:         1409696 kB
Buffers:          961452 kB
Cached:          2347236 kB
SwapCached:            0 kB
Active:          3124752 kB
Inactive:        2781308 kB
Active(anon):    2603376 kB
Inactive(anon):   309056 kB
Active(file):     521376 kB
Inactive(file):  2472252 kB
Unevictable:        5864 kB
Mlocked:            5880 kB
SwapTotal:       1998844 kB
SwapFree:        1998844 kB
Dirty:              7180 kB
Writeback:             0 kB
AnonPages:       2603272 kB
Mapped:           788380 kB
Shmem:            311596 kB
Slab:             200468 kB
SReclaimable:     151760 kB
SUnreclaim:        48708 kB
KernelStack:        6488 kB
PageTables:        78592 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     6082768 kB
Committed_AS:    9397536 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      420204 kB
VmallocChunk:   34359311104 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB                                                                                                                           
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       62464 kB
DirectMap2M:     8316928 kB

Check the values of Memtotal, Memfree, buffers, Cached, Swaptotal, Swapfree.
They indicate same values of memory usage as the free command.


3. Vmstat


The Vmstat command with the S option, lays out the memory usage statistics much like the proc command. Here are an example


$ vmstat -s
      8167848 K total memory
      7449376 K used memory
      3423872 K active memory
      3140312 K inactive memory
       718472 K free memory
      1154464 K buffer memory
      2422876 K swap cache
      1998844 K total swap
            0 K used swap
      1998844 K free swap
       392650 non-nice user cpu ticks
         8073 nice user cpu ticks
        83959 system cpu ticks
     10448341 idle cpu ticks
        91904 IO-wait cpu ticks
            0 IRQ cpu ticks
         2189 softirq cpu ticks
            0 stolen cpu ticks
      2042603 pages paged in
      2614057 pages paged out
            0 pages swapped in
            0 pages swapped out
     42301605 interrupts
     94581566 CPU context switches
   1382755972 boot time
         8567 forks
$


The top few lines indicate total memory, free memory etc and so on.


4. Top command


The top command is a generally used to check memory and CPU usage per process. However it also reports total memory usage and can is used to monitor the total RAM usage. The header on output has the required information. Here is a sample output


top - 15:20:30 up  6:57,  5 users,  load average: 0.64, 0.44, 0.33
Tasks: 265 total,   1 running, 263 sleeping,   0 stopped,   1 zombie
%Cpu(s):  7.8 us,  2.4 sy,  0.0 ni, 88.9 id,  0.9 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   8167848 total,  6642360 used,  1525488 free,  1026876 buffers
KiB Swap:  1998844 total,        0 used,  1998844 free,  2138148 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND                                                                                 
 2986 enlighte  20   0  584m  42m  26m S  14.3  0.5   0:44.27 yakuake                                                                                 
 1305 root      20   0  448m  68m  39m S   5.0  0.9   3:33.98 Xorg                                                                                    
 7701 enlighte  20   0  424m  17m  10m S   4.0  0.2   0:00.12 kio_thumbnail


Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the memory. The buffer and the cache information are present here too, like the command.


5. Htop


Similar to the top command, the Htop command also shows memory usage along with various and other details.






The header on top shows CPU usage along with RAM and swap usage with the corresponding figures.


RAM Information


To find out hardware information for the installed RAM, use the Demidecode command. It reports lots of information about the installed RAM memory.


$ sudo dmidecode -t 17
# dmidecode 2.11
SMBIOS 2.4 present.

Handle 0x0015, DMI type 17, 27 bytes
Memory Device
        Array Handle: 0x0014
        Error Information Handle: Not Provided
        Total Width: 64 bits
        Data Width: 64 bits
        Size: 2048 MB
        Form Factor: DIMM
        Set: None
        Locator: J1MY
        Bank Locator: CHAN A DIMM 0
        Type: DDR2
        Type Detail: Synchronous
        Speed: 667 MHz
        Manufacturer: 0xFF00000000000000
        Serial Number: 0xFFFFFFFF
        Asset Tag: Unknown
        Part Number: 0x524D32474235383443412D36344643FFFFFF


Provided information includes the size (2048MB), type (DDR2), speed (667 Mhz) etc.


Summary


All of the above mentioned commands work from the terminal and does not have a GUI. When working in a desktop with a GUI, it's much easier to use a GUI tool with graphical output. The most common tools is gnome-system-monitor on GNOME and
Ksysguard on KDE. Both provide resource usage information about CPUs, RAM, swap and network bandwidth in a graphical and easy to understand V Isual output.



5 commands to check memory usage on Linux


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.