Linux Memory Management: A Deep Dive into RAM
Overview
If you’ve ever wondered what RAM actually does, or what it has to do with Linux memory management, consider it your server’s short-term memory. RAM temporarily stores the data and instructions your system needs so it can access them quickly. When your website loads a page, processes a checkout, or runs a task, the data required is stored in RAM as the CPU processes the request.
Linux proactively manages memory and it doesn’t like leaving RAM unused. Instead, it fills extra space by caching frequently used files and data. This caching makes your system faster, and if a new program needs that memory, Linux automatically clears out what’s not required. The more RAM your server has, the more tasks it can handle at once without slowing down. When your RAM fills up, Linux can move some of that data into a special space on your storage drive called swap.
Parameters / Features
The following are key Linux terms used to describe the current state of the system memory. These include terms like used, free, swap, buffers, and cache. The free command is the most common tool to check memory usage quickly.
| Category | Term | Definition |
|---|---|---|
| Physical RAM | Total RAM | The total amount of physical memory installed on your server. |
| Used RAM | Memory currently occupied by running processes. | |
| Free RAM | Memory that is completely unused and immediately available. | |
| Available RAM | The memory that new applications can use without triggering a swap. (This is your key performance indicator). | |
| System Cache | Cache | Temporary memory Linux uses to hold data before writing it to disk. |
| Buffers | Memory used to store frequently accessed data ready for faster retrieval. | |
| Shared Memory | Memory that multiple processes can access simultaneously. | |
| Virtual Memory | Swap | Storage space used as a temporary extension of physical RAM. |
Swap
Swap is a portion of the server’s storage space that Linux uses as an extension of physical RAM. It provides virtual memory, allowing the operating system to move less frequently used data from RAM to disk when physical RAM is full or under pressure. This allows processes to continue running even when RAM has been exhausted. This frees up RAM for more active processes and prevents out-of-memory errors. If you notice your critical applications are running slowly with no available RAM and heavy swap usage, it may be time for a RAM upgrade. Read our help doc on swap to find out more.
Examples
Memory Inspection
The free command is helpful when checking the state of your system’s RAM. The following are several options (also called ‘flags’) that help make the command’s output more user friendly:
- -h: Generate human-readable output.
- -b, -k, -m, -g: Display memory in bytes, KB, MB, or GB.
- -t: Show total memory. (RAM + Swap)
Example output of the free command used to check memory usage:
~]$ free -h
total used free shared buff/cache available
Mem: 16G 8G 2G 1G 6G 6G
Swap: 4G 0G 4GIn this example, the server has 16 GB of RAM installed. 8 GB is actively in use by the programs and services that are running right now. Another 2 GB is free and ready to use. Processes are sharing 1 GB of memory for common data, while Linux is using around 6 GB for buffers and cache. By actively keeping essential files and data nearby, Linux wastes no RAM and makes things load faster.
There’s also 6 GB of RAM available for new tasks when they start. Available memory is RAM that new applications can use without triggering a ‘swap’. (In the free command output, this is the column you should monitor for overall system health.) The server has 4 GB of swap space configured, but it isn’t using any.
Swap command Examples
Regularly monitoring your system’s swap usage can reveal if insufficient RAM is degrading performance. If swap activity is consistently high, you may need to add more RAM.
Check if swap is enabled with the swapon command:
swapon --showTo see exactly which processes are actively using swap space (which can help you pinpoint resource-heavy applications), use this advanced one-line command:
for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | sort -k 2 -n -r | lessspamd 111228 kB
mariadbd 63128 kB
run-script 33096 kB
named 26648 kB
systemd-journal 20908 kB
httpd 20444 kB
httpd 18436 kBClick here to find more info on identifying which processes are using swap memory.
Common Use Cases
RAM plays a key role in almost every part of your server’s performance. Common use cases include:
- File caching: Store frequently accessed files in memory, reducing disk reads and speeding up response times.
- Running web applications: Caches the code, database connections, and data your site uses most often ready for quick access.
- Database operations: MariaDB, MySQL, and PostgreSQL use RAM heavily for caching and sorting, which helps database queries run faster.
- Handling traffic spikes: Extra RAM helps your site stay responsive when many visitors arrive at once.
- Application performance: More RAM allows for smoother multitasking and fewer slowdowns.
Use cases of the free command
- Monitoring memory on Liquid Web VPS or Dedicated Servers and Nexcess plans or servers.
- Determine if server performance issues are RAM-related.
- Planning upgrades for high-traffic websites, databases, or applications.