Cookie

5 commands to control ram on Linux

Published by TheJoe on

Estimated reading time: 4 minutes

Caution


This article was published more than a year ago, there may have been developments.
Please take this into account.

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”

The command 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

The option -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_ Path “/proc/meminfo”

In the file /proc/meminfo contains all the information about the memory of the connected machine. To read it we will have to launch the command cat. This command prints the contents of a given file to a terminal, as in the following example.

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 MemTotal, MemFree, Buffers, Cached, SwapTotal, SwapFree. They indicate the same values ​​as the command free.

3_ the command “vmstat”

The command 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 sy,  0,0 ni, 71,5 id,  8,1 wa,  0,0 hi,  0,3 si,  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.

Look here:  Tutorial: Newsprint retro with Gimp

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 dmidecode.

$ 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

Conclusions

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.


TheJoe

I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.

4 Comments

Jan · 22 March 2024 at 11:10 AM

Hi Joe,
i found the same translation errors, but i recognized them as such. And i can differenciate between translation errors and content quality. That means: yours is one of the best collections of Linux commands to get infos on RAM usage. Many thanks for that!
BTW: You have reacted perfect on Pauls rude comment – i would have reacted less kind…;-)

    TheJoe · 24 March 2024 at 1:44 PM

    Hi Jan, thank you for commenting, for being so kind (and thank you for understanding that I don’t speak German at all… eheh).
    Paul has been rude to me, it’s true… but you both had the same issue on the site. This pushed me to look for a solution and seems that I’ve found one. Now no preformatted or code tags will be translated anymore.

Paul · 1 February 2023 at 7:30 PM

What a load of rubbish.
The command “frei” doesn’t exist at all, as well as “Top”. I understand that “free” and “top” was ment.
The command “/proc / meminfo” doesn’t exist in any language at all. Everything is wrong here, /proc/meminfo is a path, not a command. And with the spaces it’s just totally garbage.

If you don’t want to or can’t check your posts ( including the translated versions), it would be better to be modestly silent instead of leading help seekers down the wrong path.

    TheJoe · 1 February 2023 at 8:33 PM

    Hey, Paul! How are you tonight? What a rude comment, I’ve never received such a rude comment!

    There’s an error in the post: /proc/meminfo is a path, not a command… I’m going to correct it right now. But if you allow me to, the best way to point something to someone you don’t know would be: “Hi there, I’ve spotted a big mistake in your post. Please doublecheck the part where you write about /proc/meminfo. It’s a path, not a command. Cheers.”.

    Coming to the second point… You’re absolutely right: I don’t doublecheck my posts in different languages, simply because I don’t even know a word of German (or Russian). But you don’t seem to be a complete noob, aren’t you? At least you know what the commands “free” and “top” do. Do you think we can collaborate on writing the same articles in our respective languages? Or you can just review my posts for the German audience.

    There’s no money involved, we shall do it for the community.

    Feel free to contact me with a comment, or via the form.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.