New Patches Aim To Lower Linux Memory Use For Swap, Slightly Improve Performance
#Infrastructure

New Patches Aim To Lower Linux Memory Use For Swap, Slightly Improve Performance

Hardware Reporter
3 min read

Tencent's Kairui Song proposes kernel patches that cut swap metadata memory consumption by 30% while delivering measurable performance gains under memory pressure.

A new patch series from Tencent engineer Kairui Song is targeting the Linux kernel's swap subsystem with dual benefits: significant memory savings and modest performance improvements. The work, posted to the Linux kernel mailing list, restructures how the kernel tracks swap usage, eliminating a static allocation that has existed in the swap code for years.

The Memory Problem

The core issue lies in the swap_map array, a static data structure that tracks how many times each swap slot is referenced. In current kernels, this array is pre-allocated based on the maximum swap device size, creating a fixed memory overhead regardless of actual usage.

For a 1TB swap device, the static swap_map consumes approximately 256MB of kernel memory. While large swap files are uncommon on desktops, they're standard practice in cloud environments and data centers where VMs may have terabytes of ephemeral swap space provisioned. Even on smaller systems, this overhead can be substantial relative to available RAM.

The Solution: Dynamic Swap Tables

Song's patches remove the static swap_map entirely. Instead, the kernel now uses the existing swap table infrastructure to store reference counts directly. This approach has several advantages:

  1. Memory savings: The 30% reduction in swap metadata memory translates to real-world savings. For a 1TB swap device, that's 256MB freed for actual application data.
  2. Scalability: The dynamic approach grows with actual usage rather than maximum potential.
  3. Simplicity: It leverages existing infrastructure rather than adding new complexity.

Memory modules

Performance Impact

While memory savings are the primary goal, the patches also deliver measurable performance improvements. Under global memory pressure—when the system is actively swapping—benchmarks show:

  • Kernel builds: 1.6% to 2.4% faster on x86_64 and ARM64 VMs with limited RAM
  • Redis/Valkey: Approximately 1.5% improvement

These gains come from reduced cache pressure. With less memory tied up in swap metadata, more RAM remains available for page cache and application data, reducing the frequency of expensive swap operations.

Context and Trade-offs

This work continues a broader trend in Linux memory management toward dynamic, usage-based allocations. Similar changes have appeared in other subsystems, like the recent work on dynamic dentry caches and variable-length inode tables.

However, there are trade-offs. Dynamic allocation introduces allocation latency that didn't exist with static arrays. Song's implementation appears to handle this efficiently by piggybacking on existing swap table operations, but it's a different performance profile that requires careful benchmarking.

The patches are also incremental. The 30% savings come from removing the static swap_map, but Song mentions another 512MB of potential savings from ongoing work. This suggests the current series is the first phase of a larger optimization effort.

Real-World Impact

For most desktop users, these changes will be invisible but beneficial. The memory savings might free up enough RAM to avoid a swap operation in edge cases, and the performance improvements, while modest, are essentially "free"—no configuration changes required.

For server operators and cloud providers running thousands of VMs, the impact is more substantial. A 256MB per-VM savings adds up quickly at scale. If you're running 1,000 VMs with 1TB swap each, that's 250GB of memory reclaimed across your fleet.

Availability

The patches are currently under review on the Linux kernel mailing list. Assuming no major issues arise, they're likely to be merged for Linux 6.14 or 6.15. The work builds on recent improvements to the swap subsystem, including the swap cache optimizations merged in 6.12.

For homelab builders and performance enthusiasts, this is another example of the kernel's ongoing evolution. Every percentage point matters when you're pushing hardware limits, and these patches deliver measurable improvements without requiring any changes to your configuration or workload.

The broader pattern here is clear: the Linux kernel is moving away from static, one-size-fits-all allocations toward dynamic structures that adapt to actual usage. It's a philosophy that makes Linux more efficient across the entire range of deployments, from Raspberry Pis to hyperscale data centers.

Comments

Loading comments...