# Managing Swap Space in Linux | Cycle.io

Canonical URL: https://cycle.io/learn/managing-swap-space

## Managing Swap Space in Linux.

Swap space is an essential part of Linux memory management. It acts as an overflow area when the physical RAM is fully utilized, helping to prevent system crashes and ensuring smooth performance. This guide explains what swap space is, why it's important, and how to manage it effectively in a Linux environment.

### What is Swap Space?

Swap space is a dedicated area on a disk that is used to extend the virtual memory available to the system. When the system's RAM is fully utilized, inactive pages of memory are moved to the swap space, freeing up RAM for active processes.

#### Types of Swap Space

-   **Swap Partition**: A separate partition on a disk dedicated solely to swap.
-   **Swap File**: A file on an existing file system that is used as swap.

#### Importance of Swap Space

Swap space is crucial for preventing out-of-memory errors, which can lead to application crashes or system instability. It allows the system to handle memory-intensive tasks more effectively, especially when physical RAM is limited.

Guides Docs

Configuring Container Resource Limits and Reserves

Learn more about how to configure container resource limits and reserves on the Cycle platform.

<https://cycle.io/docs/guides/configuring-container-resources>

### Checking Swap Space

You can check the current swap space on your system using the `free` command:

```bash
free -h
```

Or, for more detailed information:

```bash
swapon --show
```

### Creating Swap Space

#### Creating a Swap Partition

1.  **Create a Partition**: Use `fdisk` or `parted` to create a new partition and set its type to `swap`.
2.  **Format the Partition**:

```bash
sudo mkswap /dev/sdaX
```

1.  **Enable the Swap Partition**:

```bash
sudo swapon /dev/sdaX
```

1.  **Make it Permanent**: Add the swap partition to `/etc/fstab`:

```bash
/dev/sdaX none swap sw 0 0
```

Learn more about [partitioning disks in Linux](https://cycle.io/learn/partitioning-disks-in-linux).

#### Creating a Swap File

1.  **Create the Swap File**:

```bash
sudo fallocate -l 2G /swapfile
```

1.  **Set the Correct Permissions**:

```bash
sudo chmod 600 /swapfile
```

1.  **Format the File as Swap**:

```bash
sudo mkswap /swapfile
```

1.  **Enable the Swap File**:

```bash
sudo swapon /swapfile
```

1.  **Make it Permanent**: Add the swap file to `/etc/fstab`:

```bash
/swapfile none swap sw 0 0
```

### Adjusting Swappiness

The swappiness parameter controls how aggressively the kernel swaps data to disk. It is a value between 0 and 100, where lower values reduce the use of swap.

-   **Check Current Swappiness**:

```bash
cat /proc/sys/vm/swappiness
```

-   **Set Swappiness Temporarily**:

```bash
sudo sysctl vm.swappiness=10
```

-   **Set Swappiness Permanently**: Add the following line to `/etc/sysctl.conf`:

```bash
vm.swappiness=10
```

### Managing Swap Space in Containers

In containerized environments, swap space management is often handled differently, as containers typically rely on the host system's swap. Cycle.io abstracts much of the underlying complexity, ensuring that your containers operate efficiently without needing to manually configure swap within each container.

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
