In Linux, almost all commands are without a GUI. When running on access servers, only the shell. So today we are going to show you the commands that can be used to check the memory usage on a Linux system. Memory includes RAM and swap.
It is often important to check shared memory and process memory usage on servers, as well as unused resources. If you are running a web server, then the server must have enough memory to serve site visitors. If this does not happen, the site will become very slow or even become inaccessible when there is a surge in traffic, simply because memory is not enough. The same as what happens on a desktop computer.
1. The free command
The free command is the simplest and easiest to use command to check memory usage on Linux. Here’s 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 megabytes. The total total RAM of Linux is 7976 MB installed in the system, that is, 8 GB. The used column shows the amount of RAM that can be used on Linux, in our case it will be about 6.4 GB. The catch here is the cached and buffers columns. The second line says 4.6 GB is free. This is the free memory on the first line with the addition of buffers and the amount of cache memory.
Linux has a habit of caching to improve performance so that memory can be freed and used as needed. The last line is swap, which in this case is completely free.
2. / proc / meminfo
The next way to check memory usage is to read / proc / meminfo. Be aware that the / proc filesystem contains no real files. They are virtual files that contain dynamic information about the kernel and 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 MemTotal, MemFree, Buffers, Cached, SwapTotal and SwapFree. They point to the same memory usage values in the free command.
The vmstat command, with the -s option, shows memory statistics much like the Proc command. Here’s 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.
The top command is commonly used to check memory and cpu for each process. However, it also reports total memory usage and can be used to monitor overall memory usage. The output of the result has the necessary 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 out KiB Mem and KiB Swap in the header. They indicate total, used and free memory space. Buffer and cache information is present here as well as the free commands.
5. The htop command
Like the free command, the htop command also shows memory usage along with other details.
The heading at the top shows the CPU usage with RAM and swap usage with corresponding numbers.
Memory information (RAM)
Use the dmidecode command to find out the hardware information about the installed RAM. The command displays detailed information about the installed RAM.
$ 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
Information includes size (2048MB), type (DDR2), speed (667 MHz), etc.
All of the above commands work from the terminal and do not have a graphical interface. When working on a desktop with a graphical interface, it is much easier to use a graphical tool with graphical output. The most common tools are gnome-system-monitor in gnome and KSysguard in KDE. Both provide information about resource usage about CPU, RAM, exchange and network bandwidth in graphical mode and easy-to-understand visual conclusions.