Created
February 10, 2025 22:07
-
-
Save juanfdovilla/fe22a97f973fa9bc94620b53d3b3a1dd to your computer and use it in GitHub Desktop.
How to Add Swap Memory in Ubuntu 24.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| How to Add Swap Memory in Ubuntu 24.04 | |
| HomeUbuntuHow to Add Swap Memory in Ubuntu 24.04 | |
| Christian Wells | |
| February 5, 2025 | |
| No Comment | |
| 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 | |
| Extends System Memory | |
| Swap provides additional memory when RAM is fully utilized, preventing crashes due to memory shortages. | |
| Improves System Stability | |
| Helps maintain system performance by offloading inactive processes when RAM usage is high. | |
| Supports Hibernation (Suspend-to-Disk) | |
| Required for hibernation, as the system stores the current session in swap space. | |
| Configurable Size and Location | |
| Swap can be configured as a partition or as a swap file within an existing filesystem. | |
| Managed by the Kernel | |
| The Linux kernel automatically manages swap usage based on memory pressure. | |
| 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 | |
| Optimize System Performance | |
| Adjust the swappiness value to control how often the system uses swap. | |
| Enable Hibernation | |
| Use swap space to store system memory for hibernation mode. | |
| Extend Memory for Applications | |
| Useful for running memory-intensive applications on systems with low RAM. | |
| Improve Stability for Servers | |
| Prevents crashes on Linux servers with high memory usage. | |
| Swap Memory vs. RAM | |
| Feature Swap Memory RAM (Physical Memory) | |
| Speed Slow (disk-based) Fast (direct CPU access) | |
| Persistence Data remains until overwritten Cleared on reboot | |
| Cost Uses disk space More expensive hardware | |
| Use Case Backup memory, hibernation Active 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. | |
| Choose a Data Center: Pick a data center location that is closest to your target audience for optimal performance. | |
| 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. | |
| Finalize the Setup: Review your configuration and click “Create Instance” to deploy your instance. Your server will be ready in a few minutes. | |
| In Dashbord you will find your Instance IP | |
| 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 | |
| Step 4: Check Current Swap Usage | |
| Check if your system already has swap memory enabled and how much is being used: | |
| swapon --show | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment