Swap memory (or swap space) is a dedicated portion of storage (disk or SSD) used by an operating system to temporarily hold data when physical RAM (Random Access Memory) is full. It acts as a backup for RAM, ensuring that the system can continue running smoothly even when memory demand exceeds available physical memory.


Key Features of Swap Memory

  1. Extends System Memory
    • Swap provides additional memory when RAM is fully utilized, preventing crashes due to memory shortages.
  2. Improves System Stability
    • Helps maintain system performance by offloading inactive processes when RAM usage is high.
  3. Supports Hibernation (Suspend-to-Disk)
    • Required for hibernation, as the system stores the current session in swap space.
  4. Configurable Size and Location
    • Swap can be configured as a partition or as a swap file within an existing filesystem.
  5. Managed by the Kernel
    • The Linux kernel automatically manages swap usage based on memory pressure.
  6. Adjustable Swappiness
    • The swappiness parameter controls how aggressively the system uses swap, balancing performance and disk wear.

Advantages of Swap Memory

  • Prevents System Freezing: Helps avoid out-of-memory errors when RAM is exhausted.
  • Enhances Multitasking: Supports running multiple applications with limited RAM.
  • Allows Hibernation: Saves the current system state for later restoration.
  • Reduces RAM Pressure: Moves inactive processes to swap to free up physical memory.

Disadvantages of Swap Memory

  • Slower Than RAM: Disk-based swap is significantly slower than physical memory.
  • Can Cause Performance Issues: Excessive swapping (thrashing) can slow down the system if too many processes rely on swap.
  • SSD Wear Over Time: Continuous swap usage on SSDs can reduce their lifespan.

What You Can Do with Swap Memory

  1. Optimize System Performance
    • Adjust the swappiness value to control how often the system uses swap.
  2. Enable Hibernation
    • Use swap space to store system memory for hibernation mode.
  3. Extend Memory for Applications
    • Useful for running memory-intensive applications on systems with low RAM.
  4. Improve Stability for Servers
    • Prevents crashes on Linux servers with high memory usage.

Swap Memory vs. RAM

FeatureSwap MemoryRAM (Physical Memory)
SpeedSlow (disk-based)Fast (direct CPU access)
PersistenceData remains until overwrittenCleared on reboot
CostUses disk spaceMore expensive hardware
Use CaseBackup memory, hibernationActive processes, caching

Why Swap Memory is Important

Swap memory plays a crucial role in maintaining system stability, preventing crashes, and enabling features like hibernation. While not a substitute for physical RAM, swap is essential for handling memory-intensive workloads and optimizing system performance.

Step 1: Create a Server Instance on Shape.Host

Before adding swap memory, you need a server to host your environment. Here’s how to set up a server instance on Shape.Host:

Access Shape.Host: Log in to your Shape.Host account and navigate to the Cloud VPS section.

Launch a New Server: Click on “Create” and select the server type that best suits your project requirements.

Shape.host Resources dashboard with Create > Instance highlighted.

Choose a Data Center: Pick a data center location that is closest to your target audience for optimal performance.

Shape.host instance form with the data center location selector highlighted.

Select a Plan: Choose a hosting plan that aligns with your resource needs and budget.

Set the OS: Select Ubuntu 24.04 as your operating system.

Shape.host instance form with the operating system image selector highlighted.

Finalize the Setup: Review your configuration and click “Create Instance” to deploy your instance. Your server will be ready in a few minutes.

Shape.host instance form with the Create Instance button highlighted.

In Dashbord you will find your Instance IP

Shape.host Resources dashboard with the server IP address highlighted.

Step 2: Connect to Your Instance

Once your server is ready, you need to connect to it using SSH. Here’s how to do it:

  • Linux/macOS: Open your terminal and use the following command:
  ssh root@your_server_ip

Replace your_server_ip with the server’s IP address.

  • Windows: Use an SSH client like PuTTY. Enter your server’s IP address, specify the port (usually 22), and click “Open.” Log in with your username and password.

Step 3: Update the System

Before making any changes, it’s a good practice to update your system to ensure all packages are up to date. Run the following command:

apt update
Terminal showing APT package management commands and output.

Step 4: Check Current Swap Usage

Check if your system already has swap memory enabled and how much is being used:

swapon --show
Terminal showing memory and swap configuration commands or usage figures.

You can also check the overall memory usage, including swap:

free -h

Step 5: Create a Swap File

If your system does not have swap memory or you need to add more, you can create a swap file. For this example, we will create a 2GB swap file:

fallocate -l 2G /swapfile
Terminal showing memory and swap configuration commands or usage figures.

Verify the swap file creation:

ls -lh /swapfile

Step 6: Set Correct Permissions

Ensure that only the root user can read and write to the swap file:

chmod 600 /swapfile

Step 7: Set Up the Swap Area

Format the file as swap space:

mkswap /swapfile
Terminal showing file ownership or permission commands.

Step 8: Enable the Swap File

Activate the swap file:

swapon /swapfile

Verify that the swap file is active:

swapon --show

Check the overall memory usage again to confirm the swap file is being used:

free -h
Terminal showing memory and swap configuration commands or usage figures.

Step 9: Make the Swap File Permanent

To ensure the swap file is enabled after a reboot, add it to the /etc/fstab file:

echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

Step 10: Verify Swap Configuration

Check the swap configuration to ensure everything is set up correctly:

cat /proc/swaps

You should see your swap file listed.

Step 11: Check Memory Usage

Finally, verify the overall memory usage to ensure the swap file is active and being used:

free -h
Terminal showing memory and swap configuration commands or usage figures.

You have successfully added swap memory to your Ubuntu 24.04 system. Swap memory can help improve system performance by providing additional virtual memory when the physical RAM is fully utilized. This is particularly useful for servers with limited RAM or for running memory-intensive applications.

If you’re looking for a reliable and high-performance hosting solution for your server, consider Shape.Host Linux SSD VPS services. With fast SSD storage, scalable resources, and excellent support, Shape.Host provides the perfect environment for running your applications securely. Visit Shape.Host to learn more and get started today!