Often we hear a lot of customers asking why, when their server is largely idle, much of their RAM appears to be in use.
When RAM is not needed for other functions, your server will load frequently-accessed files into memory in order to read them more quickly. When a file is loaded into RAM, the server can access the information orders of magnitude faster than from disk. A modern SSD disk can read files at up to around 500-700 MB/second, if the files are in sequential units. However, RAM can be read at GB/second rates; or even tens of GB/second.
If the RAM becomes needed for another function, these files are quickly flushed out of memory, and the RAM becomes available for other tasks.
Visualizing Memory Usage
The most reliable method to view your memory use is run from the command line – “top.” Top is a very handy program, which will show you real-time system information.
Log into your server as root using SSH, and type the word “top” (without quotes) at the prompt. You’ll see something like the following:
top - 15:07:56 up 248 days, 4:02, 2 users, load average: 0.03, 0.06, 0.06 Tasks: 583 total, 1 running, 582 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1821892 total, 149188 free, 883204 used, 789500 buff/cache KiB Swap: 2047996 total, 1201324 free, 846672 used. 653248 avail Mem
The second line from the bottom (the one that starts with “KiB Mem”) shows your total memory usage – it displays:
- how much RAM you have (total – 2 GB in this case, rounded to 1.73 GB usable)
- how much RAM is free (free – 0.14 GB)
- and how much RAM is in use (used – 0.84 GB)
However the “free” value is not actually a complete representation of free memory, because it should also include “buff/cache”. This is where frequently-accessed files are loaded into memory, but if this memory is needed, then it is flushed and returned for use. So technically, total free RAM is:
- 0.14 GB free, plus…
- 0.84 GB used, plus…
- 0.75 GB buff/cache, equals…
- in total, 1.73 GB
For a more humorous take on this, the legendary Linux Ate My RAM! is definitely worth a read.
The bottom line (“KiB Swap”) shows your swap usage. “Swap” memory is extremely inefficient; the system is using hard disk space as virtual memory, essentially using the disk as “fake RAM.” If you see your server has low free memory, and is using a lot of swap space, this is cause for some concern – we can investigate what is taking up so much memory, and if a RAM upgrade is required.
If you see available RAM, but swap is still taking up space, as in this example, this is less of a concern. Once a file is written to swap, it tends to stay there until the space is needed for another function.
If one particular service is using up the majority of the available RAM on the server, restarting the service can sometimes help lessen the overall load. Restarting a service is a quick process that should only take a few seconds. Instructions on restarting a service via the command line can be found here: Restarting Services from the Command Line