Fundamentals 6 min read

How Linux Virtual Memory Works: Swap Partitions, Swap Files, and Tuning Tips

This article explains the role of virtual memory in Linux, covering the difference between physical and virtual memory, how swapping frees RAM, the two implementation methods—swap partitions and swap files—along with swappiness tuning and the performance benefits they provide.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How Linux Virtual Memory Works: Swap Partitions, Swap Files, and Tuning Tips

Background

Memory (RAM) is a critical component of a computer, but its capacity is limited and costly, prompting many users to explore ways to extend usable memory when the physical RAM runs out.

What Is Virtual Memory?

Virtual memory is a technique that uses disk space to emulate additional RAM, allowing the operating system to move rarely used data from physical memory to a designated swap area, thereby freeing RAM for active processes.

How Virtual Memory Works

When physical memory becomes insufficient, the kernel selects pages belonging to inactive programs and writes them to the swap area. If those programs need to run again, the data is read back into RAM, enabling continued operation without immediate crashes.

Linux Implementation

Swap Partition

A swap partition is a dedicated disk partition formatted for swapping. It is created and activated using the mkswap command to set up the swap space and the swapon command to enable it.

Swap File

If there is not enough free disk space for a separate partition, a swap file can be created on an existing filesystem. The file acts as a swap area where inactive RAM pages are stored.

Swappiness and Swap Weight

Linux allows the swap usage frequency to be tuned via the swappiness kernel parameter, which accepts values from 0 to 100. A lower value reduces swap activity, while a higher value encourages more swapping. The default and recommended value is 60.

Benefits of Using Virtual Memory

It frees up RAM occupied by rarely used data, allowing higher‑priority programs to run.

Prevents the system from running out of RAM, which would otherwise halt new processes.

Acts as a backup for RAM, effectively increasing available memory.

Enables successful hibernation by writing RAM contents to swap.

Improves overall system performance when swappiness is set appropriately.

Conclusion

Virtual memory, implemented via swap partitions or swap files, extends RAM capacity and helps avoid application crashes due to memory shortage. Properly tuning swap settings, especially the swappiness value, can be crucial for Linux servers and web services, offering performance gains and cost savings.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSwap
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.