> ## Documentation Index
> Fetch the complete documentation index at: https://wiki.lumiweb.cc/llms.txt
> Use this file to discover all available pages before exploring further.

# Swap file

> Swap when RAM runs out

Swap is a file on disk that the system uses as "spare" memory when RAM runs out. Without swap, a process that runs out of memory gets killed by the OOM-killer — the service crashes. With swap, the kernel pushes inactive pages out to disk and the process keeps running.

<Warning>
  Swap is not a replacement for RAM. Disk is tens of times slower than RAM. Swap saves you from sudden crashes and smooths out spikes, but if the server constantly "lives" in swap, that's a sign it's genuinely short on memory.
</Warning>

## Check the current state

First, see whether swap already exists and how much free memory there is:

```bash theme={"system"}
free -h
swapon --show
```

`free -h` shows the `Mem` (RAM) and `Swap` lines. If `swapon --show` outputs nothing, there's no swap.

## Create a swap file

Connect to the server over SSH as `root` (the IP and password are in the server card in the bot) and run the commands step by step.

<Steps>
  <Step title="Create a file of the size you need">
    The quick way is `fallocate`:

    ```bash theme={"system"}
    fallocate -l 2G /swapfile
    ```

    If `fallocate` isn't available or the file system doesn't support it, use `dd`:

    ```bash theme={"system"}
    dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
    ```

    `count=2048` with `bs=1M` gives exactly 2 GB.
  </Step>

  <Step title="Lock out access for others">
    The swap file must be accessible to root only, otherwise the system will refuse to enable it:

    ```bash theme={"system"}
    chmod 600 /swapfile
    ```
  </Step>

  <Step title="Format it as swap">
    ```bash theme={"system"}
    mkswap /swapfile
    ```
  </Step>

  <Step title="Enable it">
    ```bash theme={"system"}
    swapon /swapfile
    ```

    Check that swap has appeared:

    ```bash theme={"system"}
    swapon --show
    free -h
    ```
  </Step>

  <Step title="Enable it at boot">
    So that swap is mounted after a reboot, add a line to `/etc/fstab`:

    ```bash theme={"system"}
    echo '/swapfile none swap sw 0 0' >> /etc/fstab
    ```

    Check that the line was added only once: `grep swapfile /etc/fstab` — duplicates in `fstab` cause errors at boot.
  </Step>
</Steps>

## How much to allocate

There's no hard rule; here's a rough guide:

| Server RAM   | Recommended swap      |
| ------------ | --------------------- |
| 1–2 GB       | equal to RAM (1–2 GB) |
| 4–8 GB       | 2–4 GB                |
| 16 GB and up | 2–4 GB is enough      |

The logic: on the smaller plans (VPS-0, VPS-1 with 1–2 GB of RAM) swap genuinely helps you out; on the larger ones it's more of "insurance" against rare spikes, so there's no point chasing size.

## Tune swappiness

`vm.swappiness` (0–100) determines how eagerly the kernel reaches for swap. The default is usually `60`. For a server, it makes sense to lower it to `10` — then swap is used only when memory really runs short, not "just in case".

Current value:

```bash theme={"system"}
cat /proc/sys/vm/swappiness
```

Apply it right away (until reboot):

```bash theme={"system"}
sysctl vm.swappiness=10
```

Make it permanent — add it to `/etc/sysctl.conf`:

```bash theme={"system"}
echo 'vm.swappiness=10' >> /etc/sysctl.conf
```

<Note>
  Lumi's disks are on NVMe, so swap here is noticeably faster than on regular HDD/SATA-SSD. But even fast NVMe is still slower than RAM. If you're constantly hitting the memory ceiling and swap is "on fire", that's not a reason to inflate the file — it's a reason to [upgrade your plan through support](https://t.me/lumisup_robot).
</Note>

## Where to next

<CardGroup cols={2}>
  <Card title="Run an LLM on the server" icon="robot" href="/en/vps/llm">
    Ollama and local language models on a VPS.
  </Card>

  <Card title="VPS not working" icon="triangle-exclamation" href="/en/vps/troubleshooting">
    Connection issues, load, and other common situations.
  </Card>
</CardGroup>
