Swap File is a type of space on a hard drive (or other storage device) that is used when the physical RAM (Random Access Memory) is full. When the system needs more memory resources and the RAM is fully occupied, it can “swap” out inactive pages of memory and write them to the swap file. This process is known as “paging” or “swapping.”
– Virtual Memory: Swap space is a key component of virtual memory systems found in modern operating systems. Virtual memory allows an operating system to compensate for physical memory shortages, temporarily transferring data that is not actively in use to disk storage.
– Page File System: Unlike a dedicated swap partition, a swap file can exist as a regular file within a filesystem. It is managed by the kernel’s paging (or virtual memory) system, which handles the mapping between physical RAM and virtual address space.
– Swapping and Paging: When the system decides that it needs more RAM for active processes, it can choose less active data and move it to the swap file. This process is known as “swapping” or “paging out.” Conversely, when the data is needed again, it can be “paged in” from the swap file back into RAM.
– Swappiness Parameter: Linux kernels have a tunable parameter called swappiness, which controls the relative weight given to swapping out of runtime memory, as opposed to dropping pages from the system page cache. A high swappiness value can make the kernel more aggressive in swapping memory pages out, while a low value can make it more conservative.
– I/O Latency: Swapping increases I/O operations and can significantly impact system performance because reading from and writing to a disk is orders of magnitude slower than accessing RAM. SSDs can mitigate the performance penalty of swap operations due to their faster read/write speeds compared to HDDs.
– Memory Pressure and OOM Killer: When a system is under severe memory pressure and the swap file along with RAM is exhausted, the Linux kernel may invoke the Out-Of-Memory (OOM) Killer to forcibly terminate processes to free up memory.
– Filesystem and Allocation: The swap file is typically allocated with a fixed size to prevent fragmentation on the disk. When creating a swap file, it’s generally recommended to allocate it contiguous blocks on the filesystem to optimize swap performance.
– Kernel Handling: The kernel handles swap space using its own internal data structures and algorithms. It tracks which memory pages are in the swap file and which pages are in physical memory, handling the complexity of deciding when to swap pages in and out.
Have you ever changed the parameters for the swap file? What was the max limit that you have used in your program to see full RAM usage? How much did you increase the swap file size to?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Article Written By: Yashwanth Naidu Tikkisetty
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
