December 5, 2026

Linux Swap File: Creating One Correctly, What swappiness Really Controls, and a Test of Running Out of Memory

An overflowing desk with the extra papers filed in an open cabinet drawer beside it

Swap is the part of Linux memory management people most often switch off on purpose and then miss. Without it, a program that asks for more memory than the system can give is killed outright. With it, the same program slows down instead of dying. The difference was tested directly: a Python process allocating 400 MB inside a 200 MB memory limit finished normally with a swap file active and was killed by the kernel without one. All tests ran on Ubuntu 24.04 with an ext4 root filesystem, using a 1 GB swap file and a memory-limited control group to create pressure on demand.

Video: Learn Linux TV on YouTube

Creating the swap file

Four commands

fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

fallocate reserves the space, chmod makes it readable only by root, mkswap writes the swap header, and swapon hands it to the kernel. On the test machine the whole sequence took a fraction of a second, and mkswap reported:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)

Checking it worked

$ swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2

free -m now showed a Swap line of 1023 MB, all of it free. Swap is only used when memory runs short, so an idle machine with plenty of RAM shows 0 used.

Keeping it after a reboot

swapon lasts until the next boot. To make it permanent, the file goes in /etc/fstab with swap as both the mount point and the type:

/swapfile  none  swap  sw  0  0

The earlier post on /etc/fstab covers how to test a new line before rebooting.

RAM memory module

Two mistakes the tools catch

A file with holes

A swap file cannot be a sparse file, one whose size is reserved on paper but not actually allocated on disk. The swapon manual explains why: the kernel expects to write to the swap file directly, without the filesystem’s help, which is a problem on files with holes or on copy-on-write files such as those on Btrfs. It notes that cp and truncate create files with holes. The test confirmed it. A 64 MB file made with truncate drew a warning from mkswap, and swapon refused it:

mkswap: /tmp/sparse.swap contains holes or other unsupported extents.
        This swap file can be rejected by kernel on swap activation!
swapon: /tmp/sparse.swap: skipping - it appears to have holes.

fallocate versus dd

The same manual adds that preallocated files from fallocate may also be treated as having holes depending on the filesystem, and calls dd writing zeros the most portable method. On ext4 in this test, the fallocate file was accepted without complaint. On another filesystem, the slower and safer form is:

dd if=/dev/zero of=/swapfile bs=1M count=1024

Permissions

A swap file can contain anything that was in memory, including passwords and keys. A test file left at the default 644 permissions drew warnings from both tools:

mkswap: /tmp/perm.swap: insecure permissions 0644, fix with: chmod 0600 /tmp/perm.swap
swapon: /tmp/perm.swap: insecure permissions 0644, 0600 suggested.

Both still went ahead. The warning is the only protection, so the chmod 600 step matters.

What happens when memory runs out

The test

To create memory pressure without exhausting the whole machine, a memory control group was limited to 200 MB, and a small Python script inside it allocated 400 MB and touched every page so the memory was really used.

With swap

allocated 400 MB in 0.44 s
  swap in cgroup MB: 204  rss MB: 198

The process stayed within its 200 MB of real memory and pushed the overflow, 204 MB, out to the swap file. It finished and exited normally.

Without swap

With the swap file turned off and the same script run again:

Killed
  rc=137

Exit code 137 means the process was killed with signal 9. The kernel log recorded the reason:

Memory cgroup out of memory: Killed process 29704 (python3) total-vm:445216kB, anon-rss:204160kB

No swap means nowhere to put the extra 200 MB, so the kernel’s out-of-memory killer ended the process. On a whole machine without limits, the same thing happens to whichever process the kernel picks once real memory runs out.

Weighing scale

What swappiness controls

The kernel’s definition

vm.swappiness is often described as “how eager Linux is to swap.” The kernel documentation is more precise. It defines swappiness as the rough relative IO cost of swapping and filesystem paging, on a scale from 0 to 200. At 100, the kernel treats both as equally expensive and puts equal pressure on the page cache and on swap-backed memory. Lower values tell it swap is more expensive, higher values cheaper. The default is 60, which the test machine confirmed in /proc/sys/vm/swappiness.

The trade-off it sets

When memory gets tight, the kernel has two things it can drop or move: cached file data, which can be re-read from disk later, and program memory, which can only go to swap. Swappiness tilts that choice. A low value keeps program memory in RAM longer and sacrifices file cache first. A high value is willing to swap out idle program memory to keep more files cached. The documentation adds that values above 100 can make sense for in-memory swap such as zram, where swapping is cheaper than reading files from disk.

What 0 does

The documentation says that at 0, the kernel will not start swapping until free and file-backed pages fall below the high watermark in a zone. That is “swap as a last resort,” not “never swap.”

swappiness 0 inside a control group

A different meaning

The test showed how much context matters. On the older cgroup v1 memory controller used in the sandbox, each control group has its own memory.swappiness file. With it set to 0 for the test group, the 400 MB script behaved exactly as if there were no swap at all:

  swap in cgroup MB: 0  rss MB: 0
  rc=137

The process was killed even though 1 GB of swap sat unused. Inside that group, 0 meant no swapping, so the overflow had nowhere to go. Setting the group back to 60 made the same script succeed again. Anyone setting swappiness to 0 to “stop swapping” should know which one they are setting, and that the result may be an out-of-memory kill rather than a slower program.

Changing the system value

The system-wide value can be changed at once with sysctl vm.swappiness=10 and made permanent with a line in a file under /etc/sysctl.d/. A lower value like 10 is a common choice on desktops that have plenty of RAM and want programs to stay responsive; servers with large file caches often leave the default. The documentation’s own advice is that the best value needs experimentation and depends on the workload.

Printed circuit board

How much swap to create

No single answer

Old rules like “twice the RAM” date from machines with far less memory. The test shows what swap actually buys: a cushion that turns an out-of-memory kill into slowdown. A modest swap file, enough to absorb a spike, covers that purpose on most systems. Hibernation is the exception, since it writes the contents of RAM to swap and needs at least that much space.

More than one swap area

Linux can use several swap files or partitions at once, and each has a priority. To see how that plays out, two 300 MB swap files were created with dd and enabled with different priorities:

swapon -p 10 /var/tmp/s1.swap
swapon -p 5 /var/tmp/s2.swap

The same memory-limited script, this time allocating 450 MB, filled only the higher-priority file:

NAME             TYPE SIZE   USED PRIO
/var/tmp/s1.swap file 300M 254.8M   10
/var/tmp/s2.swap file 300M     0B    5

The lower-priority file stayed empty until the first one would be full. That makes priorities useful for putting a fast SSD swap file ahead of a slower one on a spinning disk. Two areas with equal priority are used in turn, spreading the writes between them.

Turning swap off while it is in use

swapoff does not throw away what is in swap; it has to put every page somewhere. With 255 MB in the high-priority file and the process still running, swapoff on that file took 1.9 seconds, and afterward the lower-priority file held 264.6 MB. Because the process was still limited to 200 MB of real memory, the pages could not come back into RAM, so the kernel moved them to the remaining swap area instead. Had there been no other swap and no free memory to receive them, the command could not have completed cleanly. Before turning swap off on a busy machine, it is worth checking with free -m that the used swap will fit in free memory.

Watching it

swapon --show and free -m show how much is in use. Swap that fills up and stays full under normal load means the machine needs more memory, not more swap. Swap that is occasionally touched during spikes is doing its job. The earlier post on limiting a process’s memory with cgroups covers the controls used in this test.

Sources

  • swapon(8), Linux manual page (man7.org): https://man7.org/linux/man-pages/man8/swapon.8.html
  • Documentation for /proc/sys/vm/, The Linux Kernel documentation: https://docs.kernel.org/admin-guide/sysctl/vm.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *