> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# This Simple Change Improved the Performance of My Homelab Running on Raspberry Pi
- URL: https://itsfoss.com/pi-swap-increase/
- Published: 2024-11-17T04:04:30.000Z
- Updated: 2024-11-17T04:04:30.000Z
- Description: My struggling Raspberry Pi got a performance boost by a small change in the memory configuration.
- Author: Abhishek Kumar
- Tags: Homelab, Raspberry Pi

Running a homelab on a Raspberry Pi can be a rewarding yet challenging experience. My journey began with a [Raspberry Pi 4 ](https://www.raspberrypi.com/products/raspberry-pi-4-model-b/?ref=itsfoss.com)(4 GB) hosting several [WordPress](https://wordpress.org/?ref=itsfoss.com) websites and a few [Ghost](https://ghost.org/?ref=itsfoss.com) blogs. 

Recently, I became more ambitious, aiming to expand my setup with [Nextcloud](https://nextcloud.com/?ref=itsfoss.com) for private cloud storage and [ONLYOFFICE](https://www.onlyoffice.com/?ref=itsfoss.com) to create a fully self-hosted ecosystem. This is where things got interesting and a bit messy.

Adding Nextcloud and ONLYOFFICE to the mix put a significant strain on my Pi 4, leading to slowdowns, occasional freezes, and a slew of input/output errors. 

After some investigation, **I found that the default 200 MB swap memory was entirely maxed out**. 

Curious, I thought, “Why limit it to just 200 MB? Let’s see what happens if I increase it.” **So, I expanded the swap space to 2 GB, and my Pi started running noticeably smoother.** 

Here’s how I did it, and some tips if you're considering the same.

## Prerequisites

To follow along, make sure you have:

- A Raspberry Pi running Raspberry Pi OS.
- Access to a terminal (either directly or via SSH).
- Sufficient free space on your SD card for the additional swap.

## Step 1: Turn off the current swap

Before resizing the swap file, you’ll need to turn off the active swap space. Run the following command in your terminal:

```bash
sudo dphys-swapfile swapoff
```

This disables the swap temporarily, allowing you to adjust its size.

![Turning the swap off](https://itsfoss.com/content/images/2024/11/turning-off-swap.png)

## Step 2: Edit the swap configuration file

Next, open the swap configuration file in a text editor. I prefer `nano`, but any editor will do:

```bash
sudo nano /etc/dphys-swapfile
```

Look for the line `CONF_SWAPSIZE=200` (or a similar value). This line defines the current swap size in megabytes.

![You can see the default swap is of 200MB](https://itsfoss.com/content/images/2024/11/initial-swap-size-1.png)

## Step 3: Increase the swap size

Change the value to your desired size. For instance, setting it to 2048 will give you 2GB of swap space:

```bash
CONF_SWAPSIZE=2048
```

![changing the swap value from 200MB to 2048MB](https://itsfoss.com/content/images/2024/11/increased-swap-space-to-2048.png)

Remember that the size you specify must be available on your SD card. Save your changes by pressing `CTRL + X`, followed by `Y`, then `Enter` to confirm.

## Step 4: Apply the new swap size

After updating the configuration, recreate the swap file to match the new size by running:

```bash
sudo dphys-swapfile setup
```

This command will delete the old swap file and create a new one with the size specified in the configuration file.

![reloading the changed swap file](https://itsfoss.com/content/images/2024/11/reloading-swap-setup.png)

## Step 5: Turn the wwap back on

Now, re-enable swap with:

```bash
sudo dphys-swapfile swapon
```

At this point, your new swap file is active, but for the best results, reboot your Pi to ensure all applications recognize the additional memory:

```bash
sudo reboot now
```

![htop screenshot of my pi 4](https://itsfoss.com/content/images/2024/11/my-pi4-with-2GB-swap.png)

Actual screenshot of my Pi 4 resource usage after adding 2Gb of swap

## Why Increase Swap Space?

The Raspberry Pi’s swap file acts as an overflow for RAM, giving the system extra memory by writing some data to disk when physical RAM is fully utilized. 

Although accessing swap memory is slower than RAM, it can help manage resource-intensive applications by preventing crashes or system freezes. 

However, keep in mind that an unnecessarily larger swap file can wear down your SD card over time.

## Final Thoughts

Increasing my Raspberry Pi's swap space from 200 MB to 2 GB made a noticeable difference. My Nextcloud and OnlyOffice setup has been running more smoothly, with fewer slowdowns and far less system freezing. 

However, this solution is still not perfect, SD card wear and the slower nature of swap memory mean it's more of a temporary solution than a permanent fix.

For anyone trying to push the limits of a Pi like I have, increasing swap space can provide short-term relief, but it’s essential to consider upgrading to hardware with more RAM if you're regularly maxing out your resources. 

In my case, I'm exploring x86 servers to handle my containers and self-hosted applications more robustly, and I’ll cover that journey in a future article.

This experience has further reinforced my love for tinkering and optimizing setups, but also reminded me of the limitations of working on minimal hardware. 

Sometimes, a modest hardware upgrade can make all the difference in creating a smoother, more reliable self-hosted ecosystem.