5 commands to control ram on Linux
In Linux there are commands pretty much for anything, because the graphical interface cannot always be visible. When working on a server and only shell access exists, everything is available with these commands. So today we will explore the commands that can be used to control memory usage on a Linux system. La memoria include RAM e swap.
It is often important to monitor memory usage and the memory used to make sure you don't run out and continue to provide user access to the server. For example a site. If you have a web server active, the server itself must have enough memory to serve site visitors. Otherwise the site will become very slow, or worse it can “go down” in traffic spikes, simply because the memory would become too little. A dust’ as happens to PCs.
But now let's see the five alternative commands:
1_ The command “free”
free it is perhaps the simplest of commands to control memory usage on Linux. Here is a quick example:
$ free -h total used free shared buff/cache available Mem: 3,8G 2,7G 157M 267M 915M 565M Swap: 2,0G 641M 1,4G
-h shows the data in a format easily readable by the human eye. The 3.8G total is the total of installed RAM (4GB). The column
used shows the ram used by the system, in this case 2.7G. The output is easily explained by itself.
2_ The command “/proc/meminfo”
The next command to check for RAM congestion is to read the file
Actually the system directory
/proc/ does not contain any files. Content are virtual files that contain dynamic information about your system and the kernel.
$ cat /proc/meminfo MemTotal: 3938504 kB MemFree: 274104 kB MemAvailable: 538012 kB Buffers: 30208 kB Cached: 592216 kB SwapCached: 37948 kB Active: 2583536 kB Inactive: 830584 kB Active(anon): 2364820 kB Inactive(anon): 609264 kB Active(file): 218716 kB Inactive(file): 221320 kB Unevictable: 32 kB Mlocked: 32 kB SwapTotal: 2097148 kB SwapFree: 1186988 kB Dirty: 320 kB Writeback: 0 kB AnonPages: 2777800 kB Mapped: 310684 kB Shmem: 182244 kB Slab: 112400 kB SReclaimable: 56800 kB SUnreclaim: 55600 kB KernelStack: 12032 kB PageTables: 67464 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 4066400 kB Committed_AS: 8606840 kB VmallocTotal: 34359738367 kB VmallocUsed: 0 kB VmallocChunk: 0 kB HardwareCorrupted: 0 kB AnonHugePages: 0 kB ShmemHugePages: 0 kB ShmemPmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 273724 kB DirectMap2M: 3819520 kB
The values relating to
SwapFree. They indicate the same values as the command
3_ the command “vmstat”
vmstat with the option
-s shows RAM usage statistics, more or less like the previous command. Here is an example:
$ vmstat -s 3938504 K total memory 3055616 K used memory 2634584 K active memory 824360 K inactive memory 228428 K free memory 35676 K buffer memory 618784 K swap cache 2097148 K total swap 933456 K used swap 1163692 K free swap 323777 non-nice user cpu ticks 1544 nice user cpu ticks 80596 system cpu ticks 506483 idle cpu ticks 24004 IO-wait cpu ticks 0 IRQ cpu ticks 1124 softirq cpu ticks 0 stolen cpu ticks 7303873 pages paged in 5460784 pages paged out 22090 pages swapped in 256081 pages swapped out 14156008 interrupts 32916209 CPU context switches 1589867624 boot time 32561 forks
4_ the command “top”
Top is generally used to check RAM and processor congestion. However, it also reports the total amount of RAM. The first lines of the output show the information that interests us:
top - 10:00:02 up 2:06, 3 users, load average: 0,87, 1,29, 1,35 Tasks: 247 total, 2 running, 202 sleeping, 0 stopped, 0 zombie %Cpu(s): 14,2 us, 5,9 his, 0,0 ni, 71,5 id, 8,1 wa, 0,0 hi, 0,3 you, 0,0 st KiB Mem : 3938504 total, 496344 free, 2890560 used, 551600 buff/cache KiB Swap: 2097148 total, 1120172 free, 976976 used. 620624 avail Mem
The interesting part are the values related to “
KiB Mem” and “
KiB Swap“. They indicate the total, the free and used memory.
5_ the command “htop”
Very similar to the command
top (not only in the name), also
htop shows us information about the RAM, but in a more readable and dynamic way.
RAM information with “dmidecode”
One of the commands that ever show the most RAM information is definitely
$ sudo dmidecode -t 17 dmidecode 3.1 Getting SMBIOS data from sysfs. SMBIOS 2.5 present. Handle 0x000D, DMI type 17, 27 bytes Memory Device Array Handle: 0x000C Error Information Handle: No Error Total Width: 64 bits Data Width: 64 bits Size: 2048 MB Form Factor: SODIMM Set: 1 Locator: M1 Bank Locator: Bank 0 Type: Reserved Type Detail: Synchronous Speed: 800 MT/s Manufacturer: Mfg 0 Serial Number: 1234-B0 Asset Tag: Not Specified Part Number: SODIMM000 Handle 0x000E, DMI type 17, 27 bytes Memory Device Array Handle: 0x000C Error Information Handle: No Error Total Width: 64 bits Data Width: 64 bits Size: 2048 MB Form Factor: SODIMM Set: 1 Locator: M2 Bank Locator: Bank 1 Type: Reserved Type Detail: Synchronous Speed: 800 MT/s Manufacturer: Mfg 1 Serial Number: 1234-B1 Asset Tag: Not Specified Part Number: SODIMM001
All the solutions mentioned are vital in all areas where the graphical interface is precluded. In case we were working on a classic computer with a gui installed the simplest software are “
gnome-system-monitor” for Gnome and “
ksysguard” by KDE.
Both show total memory, used, free, The swap, CPU and network load with an easy-to-interpret graphical interface.