All ArticlesDevOps2026-11-15T23:21:48+08:00

Migrated from KVM to LXC on Proxmox but Container Hits Swap Hard? Verify Memory Limits and Cgroup Config

After converting KVM VMs to LXC containers on Proxmox, memory pressure and swap usage spike unexpectedly because cgroup memory limits differ from VM balloon settings. The reader will know how to verify effective memory limits, check swap accounting in cgroup v2, and confirm the container is not silently OOM-killing processes.

You migrated a workload from a KVM virtual machine to an LXC container on Proxmox, gave it the same RAM number in the GUI, and now free -m inside the container shows swap usage climbing fast. The application feels sluggish, maybe processes restart without explanation. The confusion is common: KVM and LXC handle memory limits through completely different mechanisms, and a straight number-for-number migration often leaves the container with a swap allowance you never intended.

How KVM and LXC Memory Limits Differ

A KVM VM gets a fixed block of RAM from QEMU. The guest kernel sees that RAM as physical memory and manages its own swap (if you configured a swap partition inside the VM disk). The hypervisor does not inject extra swap from the host.

LXC containers share the host kernel. Memory limits are enforced by cgroups (control groups), a kernel feature that caps how much RAM and swap a process group can consume. On Proxmox, when you set an LXC container to 2048 MB of memory, Proxmox also typically configures a swap allowance — often equal to the memory value — unless you explicitly set swap to zero. That means a container configured for 2 GB of RAM might silently have 2 GB of swap available from the host's swap space. The container's processes will use that swap when memory pressure rises, and you will see the symptoms: high si/so in vmstat, latency spikes, and mysterious slowdowns.

If your KVM VM had no swap partition (or a small one), and the replacement LXC container suddenly has gigabytes of swap courtesy of cgroup defaults, the behavior difference is dramatic.

Step 1: Check What Proxmox Actually Configured

On the Proxmox host, check the container's config. Replace CTID with your container ID:

pct config CTID | grep -E 'memory|swap'

Illustrrative output format:

memory: 2048
swap: 2048

If swap is missing from the output, Proxmox may be using its default (which is typically equal to the memory value — verify this against your Proxmox version's documentation). If you see swap: 2048 and you did not want swap, that is your problem right there.

To set swap to zero from the host:

pct set CTID -swap 0

This takes effect after restarting the container. Reducing swap on a running container that is actively swapping can cause OOM kills, so plan for a brief restart window.

Step 2: Verify Effective Cgroup Limits on the Host

The Proxmox config file is the intent. The cgroup filesystem is the truth. On Proxmox 7 and later with cgroup v2 enabled, the effective limits live under /sys/fs/cgroup. The exact path depends on your Proxmox version and cgroup hierarchy. For cgroup v2, it typically looks like:

# Memory limit in bytes
cat /sys/fs/cgroup/lxc/CTID/memory.max

# Swap limit in bytes
cat /sys/fs/cgroup/lxc/CTID/memory.swap.max

# Current memory usage
cat /sys/fs/cgroup/lxc/CTID/memory.current

# Current swap usage
cat /sys/fs/cgroup/lxc/CTID/memory.swap.current

If memory.swap.max shows a large number (or max, meaning unlimited), the container can swap as much as the host allows. That is almost never what you want for a production workload that was previously running in a fixed-RAM KVM VM.

On older Proxmox installations still using cgroup v1, the paths differ: look under /sys/fs/cgroup/memory/lxc/CTID/ for files like memory.limit_in_bytes, memory.memsw.limit_in_bytes, and memory.usage_in_bytes. The memsw file controls memory+swap combined, not swap alone, which is another source of confusion.

If you are unsure whether your host uses cgroup v1 or v2, check:

mount | grep cgroup

Lines containing cgroup2 indicate v2. Lines with cgroup and specific controller names (memory, cpu) indicate v1.

Step 3: Look for Silent OOM Kills

When a container hits its memory limit and swap is exhausted (or disabled), the kernel's OOM killer terminates processes inside the container. The container itself stays running — only individual processes die. This is invisible unless you check.

From the host, check the cgroup's OOM event counter (cgroup v2):

cat /sys/fs/cgroup/lxc/CTID/memory.events

Sample output format:

low 0
high 0
max 312
oom 4
oom_kill 4
oom_group_kill 0

The oom_kill counter shows how many times the OOM killer fired inside this container's cgroup. If that number is non-zero, processes have been killed. This counter persists until the container restarts.

You can also check dmesg on the host for OOM messages:

dmesg | grep -i 'out of memory\|oom-killer\|killed process' | tail -20

The log lines will reference the cgroup path, so you can match them to a specific container ID.

Inside the container, if a database or application process was killed, you might see it in the application's own logs (MySQL error log, PHP-FPM log, etc.) as a sudden restart or "connection lost" without a clean shutdown.

Step 4: Measure Current Memory Pressure

Inside the container, free -m gives you the basics, but vmstat 1 5 is more useful for spotting active swapping:

vmstat 1 5

Watch the si (swap in) and so (swap out) columns. Non-zero so values mean the kernel is actively pushing pages to swap right now. Non-zero si means it is pulling them back. Sustained non-zero values in both columns indicate thrashing — the system is spending more time moving pages than doing useful work.

For a related deep-dive on interpreting vmstat swap columns before deciding to upgrade, see Why Your App Uses More RAM After Moving a VPS from OpenVZ to KVM, which covers a similar class of memory-behavior surprises after migration.

On cgroup v2, there is also a memory.pressure file that shows PSI (Pressure Stall Information) metrics:

cat /sys/fs/cgroup/lxc/CTID/memory.pressure

Sample output format:

some avg10=45.32 avg60=38.10 avg300=22.50 total=198273645
full avg10=12.05 avg60=9.88 avg300=5.42 total=52738291

The some line shows the percentage of time at least one task was stalled waiting for memory. The full line shows when all tasks were stalled. If avg10 on the full line is above single digits, the container is under serious memory pressure right now.

Step 5: Right-Size the Limits

Once you have the data, the fix is straightforward:

  1. If the workload ran fine in a KVM VM with no swap, set swap: 0 in the LXC config and give it the same RAM amount (or slightly more, since the container shares the host kernel and some overhead differs).

  2. If you want a small swap buffer for occasional spikes, set swap to a fraction of memory (an illustrative example: 256 MB swap for a 2048 MB container) rather than accepting the default 1:1 ratio.

  3. After changing limits, restart the container and re-run the verification steps above. Confirm memory.swap.max in the cgroup filesystem matches your intent, oom_kill counter is zero, and vmstat shows no sustained swap activity.

One thing to watch: if the host itself is low on physical RAM and overcommitting across many containers, setting swap to zero on all containers means OOM kills happen faster. That might be the correct behavior (fail fast rather than thrash), but it requires that your total container memory allocation fits within host RAM. Check host-level memory with free -m on the Proxmox node itself.

The Short Version

KVM gives a VM a fixed RAM block and the guest manages its own swap. LXC containers inherit swap from the host via cgroup limits, and Proxmox's default swap allocation is often larger than you expect. After migrating, verify the actual cgroup files on the host — not just the Proxmox GUI — check for OOM kills in memory.events, and set swap explicitly to what you actually want. The five minutes of verification saves hours of debugging mysterious process restarts.