Ubuntu swap and ZRAM: a practical memory guide
Linux uses swap when the working set grows beyond what physical RAM can comfortably hold. Swap is often misunderstood: it does not make storage equivalent to RAM, and its presence does not automatically make a computer slow. It is a pressure valve, while ZRAM changes how some of that overflow is stored and how quickly the system can recover.
Understanding Ubuntu’s swap and ZRAM begins with separating temporary pressure from real exhaustion. A browser with dozens of tabs can push a small laptop into swapping for a moment, then release those pages when a tab closes. Sustained pressure is different: storage activity may spike, applications may stall, and the kernel may eventually invoke the out-of-memory killer.
That distinction matters for everyday Australian use. A commuter in Sydney or Melbourne may keep a work laptop open across a train journey, while a household computer in Brisbane may face summer heat and occasional power interruptions. The aim is not to eliminate swap, but to keep sudden memory demand from turning into a visibly slow desktop.
How memory pressure changes system behaviour
When memory runs short, Linux first tries to reclaim memory it can obtain without much effort. Clean file-cache pages may be discarded, and less recently used anonymous memory may be moved into swap. The operating system is balancing several competing needs: keeping active applications responsive, preserving recently accessed files and avoiding an abrupt failure.
Short-lived pressure is often unnoticeable. The system may move some pages aside while a large file loads, then bring them back when the application needs them again. Once demand remains high, swap activity becomes more frequent. On a system with a slow disk, waiting for those pages can cause noticeable pauses in menus, typing and window movement.
Swap also acts as a safety margin when the kernel cannot free enough memory. Without any usable swap, an out-of-memory condition can arrive sooner, and the kernel may terminate a process to protect the rest of the system. The result can be a closed application or, in some cases, a lost session. Swap does not guarantee survival, but it provides room for the system to respond before that final step.
RAM, disk swap and virtual memory
RAM holds active application data and operating-system structures that the processor can access directly. Its capacity sets how much work can proceed comfortably at once. Virtual memory extends that apparent capacity by allowing pages to reside in swap, but those pages must be moved back into RAM before the processor can use them again.
Traditional Ubuntu swap usually lives on storage, either in a dedicated partition or in a file such as /swapfile. A swap file is simply a convenient way to reserve space without creating another partition. Solid-state drives make this much less painful than it was with mechanical disks, although sustained swapping still consumes storage bandwidth and can add latency.
For a wider explanation of virtual memory beyond the Ubuntu specifics, this broader memory overview can provide useful context. The essential distinction remains simple: RAM is fast working space, while disk swap is a slower place to keep pages until RAM becomes available again.
Swap can also contain fragments of recently used data, which makes its security worth considering. A separate, unencrypted swap partition may remain accessible even when the root filesystem is protected. For Australian organisations, the Privacy Act 1988 and Australian Privacy Principles make careful handling of personal information relevant, although neither dictates Linux swap settings. Disk encryption and a checked swap layout remain sensible local precautions.
What ZRAM changes differently
ZRAM is a compressed block device stored in RAM. Instead of writing an evicted page directly to disk, the system compresses it, places the smaller result in a ZRAM device and releases the original memory. A page occupying 4 KB might occupy roughly 1 KB after compression, although the real ratio depends entirely on the data.
Compression saves physical space, but it is not free. The processor must compress and decompress pages, using a little CPU time and therefore some power. That cost is usually modest on a modern desktop and much smaller than the delay caused by waiting for a slow storage device. ZRAM also consumes some RAM for metadata, so it redistributes memory rather than creating additional capacity.
This makes ZRAM particularly effective against bursty pressure. Opening a large application, switching between browser-heavy tasks or briefly exceeding available RAM can be handled with much less delay than disk swap would cause. Sustained demand for more memory than the machine can hold still causes trouble, however, because ZRAM cannot provide memory that neither RAM nor storage can supply.
How Ubuntu combines these mechanisms
Many current Ubuntu installations use systemd-zram-generator to create a compressed swap device during boot, commonly named /dev/zram0. Other Ubuntu versions use different arrangements, so checking the running system is more reliable than assuming a particular release has a fixed setup. Commands such as swapon --show and zramctl reveal what is actually active.
When ZRAM and disk swap are both enabled, their priorities determine which is used first. Ubuntu commonly gives ZRAM a higher priority, allowing it to absorb normal pressure while disk swap remains available for overflow or hibernation-related needs. This combination provides fast temporary relief without giving up the larger safety net of storage-backed swap.
Zswap is a separate mechanism that is sometimes confused with ZRAM. It compresses pages while they are waiting to be written into traditional disk swap. ZRAM, by contrast, provides its own compressed swap device. Ubuntu may use one, the other or both, depending on the installation and configuration, so their different purposes matter when diagnosing behaviour.
Reading what your system is doing
Start with free -h, swapon --show and zramctl. Together they show available RAM, active swap devices, their types, sizes, usage and priorities. The available figure is usually more informative than the free figure, because Linux uses spare RAM for cache and can reclaim much of it when needed. Swap usage alone does not prove a problem; sustained movement does.
vmstat 1 provides a live view of activity. The si column shows pages being brought into RAM from swap, while so shows pages being moved out. Occasional non-zero values are normal after opening a heavy application. Repeated activity while the same applications remain open suggests that the workload is consistently larger than the available memory.
For a higher-memory process, sort the process list by memory consumption and check whether one application is responsible for the pressure. Command-line tools can also reveal whether an application has failed after the kernel ran out of memory. Kernel logs searched for out of memory or oom-kill provide evidence of an OOM event, helping distinguish a single failed application from a generally undersized system.
Settings that fit the machine
Ubuntu’s defaults are a sensible starting point because they are designed to work on a wide range of hardware. The Australian market includes entry-level laptops with 8 GB of RAM, mainstream models with 16 GB and enthusiast desktops with considerably more. Memory needs depend more on the applications and number of simultaneous tasks than on a processor’s advertised speed.
ZRAM size can be adjusted in /etc/systemd/zram-generator.conf. A line such as zram-size=min(ram, 4G) gives the device a limit based on installed memory, while compression-algorithm=zstd selects a commonly supported compression method. Changes normally take effect on the next boot. A modest ZRAM allocation can smooth normal pressure without taking too much RAM away from applications.
The vm.swappiness setting influences how eagerly the kernel swaps anonymous memory. Ubuntu’s common default of 60 is not inherently wrong, and many responsive desktops benefit from a lower value such as 10 or 20. Zero discourages swapping but does not completely disable it. Persistent changes belong in a sysctl configuration file, and the resulting value should be checked after a reboot rather than assumed.
Hibernation needs separate consideration. ZRAM loses its contents when power is removed and cannot replace a suitable disk swap area for a resume image. A hibernation-capable system generally needs appropriately sized swap storage, which may be much larger than the ZRAM device. During a Brisbane summer outage, reducing bursts of storage activity may be welcome, but compression still consumes power, so energy savings should not be assumed without measurement.
The memory model to keep in mind
RAM serves the active workload, disk swap provides durable overflow, and ZRAM compresses overflow in RAM so that temporary pressure is less disruptive. Each mechanism has a different cost. RAM is fast and finite, disk swap is slower but persistent, and ZRAM trades a small amount of processor time for much quicker relief.
The lasting lesson is that swap is part of Linux’s memory management rather than evidence of a badly configured computer. Ubuntu can combine all three safely, and sensible defaults combined with a little observation usually deliver a more reliable desktop than aggressive tuning based on misconceptions.