What exactly is the Swap Space?
Linux system uses the Virtual File System (VFS) to abstract the physical memory available from the memory visible to the linux processes. The VFS is responsible for allocating, freeing and managing memory for the multiple processes running on linux.
Today's machines have RAMs of more than 2GB in almost all devices. However, memory was pretty constrained a few decades ago and it is still the case in embedded systems. In these devices, one has to deal with memory of less than a few MBs.
The VFS ensures that memory is efficiently allocated to the processes. However, in cases when there is no more memory available and the system is under intense memory pressure, the entire system may crash or the kernel may throw an Out of Memory panic.
To guard against this and to provide a little bit of a breating space to the VFS to react accordingly, the Swap space was created.
The swap space is to temporarily use the physical disk as well for memory usage. Everything happens transparently to the process. The process requests for memory as usual, and the VFS utilises the Swap space for memory, which is actually stored on the disk.
You can look at the swap space by running
free -m
or by looking at the swapfile
.
cat /proc/swaps
vmstat
is another command to track your swap space activity.
Anonymous memory
When a process reads or writes to a file, the kernel loads the content from the disk and stores it in the page cache memory. If the page isn't accessed for sometime, the kernel usually flushes the changes to the disk and removes it from the main memory.
Now let's assume that your process allocates some memory on the heap and it doesn't get used very frequently. In this case, the kernel cannot flush it to any file since there is no backing file. This is another area where the swap space is of use. The swap space can be used to back up less frequently used anonymous memory to the swap space as well.
This frees up the actual main memory for more important use cases.
To check the anonymous memory regions of a process, use this:
less /proc/<PID>/smaps
and grep by the anon
section.
Swapiness configuration
You can also configure how aggressively the data is stored in swap space via the swappiness
parameter.
Just run the below command to do so:
sudo bash -c "echo 'vm.swappiness = 15' >> /etc/sysctl.conf"
Performance impact of using the Swap
The performance of storing anything on a swap space is as bad as disk performance compared to memory. It becomes especially bad if the memory is in constant churn, i.e you are continously allocating and deallocating memory on the swap space.
One important thing to note that we observed while allocating on swap space, is that once something gets allocated on the swap, it is not removed from the swap space even if there is free memory lying around. What this means is that if you have spiky memory traffic where you routinely consume the entire memory, you may tend to store things on the swap more often than you would expect.
Do we actually need swap?
Coming from the database world, it is advised that the swap space should be disabled for all nodes, especially since the memory available to the production machines are at least more than 16GB. Most data oriented processes usually tend to store as much data in memory as possible for faster access. They also keep track of the memory usage of the system to not allocate above a certain percentage of available memory.
For example, Mongo doesn't use more than 80% of the available RAM.
Also, if data does get into the swap space, the variability of the performance across memory and disk would be difficult to debug and may result in edge cases more easily.
To turn the swap on or off, you can run
# To turn swap on
swapon -a
# To turn swap off
swapoff -a
References
Hope you liked reading the article.
Please reach out to me here for more ideas or improvements.