Installing Virtual RAM Modules

LinuxBeginner
Practice Now

Introduction

In a computer system, Random Access Memory (RAM) is a critical component that stores data for running applications. When the physical RAM is fully utilized, the operating system can use a part of the hard drive as "virtual memory" to prevent system crashes. This virtual memory is commonly known as swap space.

This lab will guide you through the process of increasing your system's virtual memory by creating and enabling a swap file. This is a common system administration task for improving performance on systems with limited physical RAM. You will learn how to check memory usage, create a swap file, activate it, and monitor its usage.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a beginner level lab with a 90% completion rate. It has received a 100% positive review rate from learners.

Access Virtual Machine Settings in Hypervisor

In this step, you will check the current memory and swap space configuration of your Linux environment. This provides a baseline before we make any changes. We will use the free command, which displays the total amount of free and used physical and swap memory in the system.

First, open the terminal. To get a human-readable output (e.g., in MB or GB), we use the -h flag.

Execute the following command:

free -h

You will see an output similar to this. The exact numbers may vary depending on the system's current state.

               total        used        free      shared  buff/cache   available
Mem:           3.5Gi       502Mi       1.2Gi       3.0Mi       1.8Gi       2.8Gi
Swap:             0B          0B          0B

In the output above:

  • Mem: This row shows the physical memory (RAM).
  • Swap: This row shows the virtual memory (swap space). As you can see, there is currently no swap space configured.

Our goal is to add a swap file to increase the total available virtual memory.

Increase RAM Allocation to 4GB

In this step, you will create a 2GB file that will be used as our swap space, effectively acting as virtual RAM. We will perform this in several sub-steps: creating the file, setting permissions, formatting it as swap, and finally, enabling it.

First, we'll use the fallocate command to instantly create a file with a specific size. We will create a 2GB file named swapfile inside your project directory.

sudo fallocate -l 2G ~/project/swapfile

Next, for security reasons, we must ensure only the root user can read and write to the swap file. We'll use chmod to set the correct permissions.

sudo chmod 600 ~/project/swapfile

Now, we need to format this file as a swap area using the mkswap command.

sudo mkswap ~/project/swapfile

You should see an output confirming the setup, including a UUID for the new swap area.

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Finally, enable the swap file with the swapon command. This makes the new virtual memory available to the system immediately.

sudo swapon ~/project/swapfile

The system is now using the newly created swap file.

Verify RAM Detection with free -h Command

In this step, you will verify that the new swap file has been successfully activated and is recognized by the system. We will use the free -h command again, just as we did in the first step.

Run the command in your terminal:

free -h

Now, the output should look different. Notice the Swap row now shows the 2GB of space we allocated.

               total        used        free      shared  buff/cache   available
Mem:           3.5Gi       543Mi       1.2Gi       3.0Mi       1.8Gi       2.7Gi
Swap:          2.0Gi          0B       2.0Gi

This confirms that you have successfully added 2GB of virtual memory to your system. The operating system can now use this space if it runs out of physical RAM.

Monitor RAM Usage with htop Command

In this step, you will learn to use htop, a popular interactive process viewer and system monitor. It provides a more detailed and user-friendly view of system resources compared to the top command.

You can run htop by simply typing its name in the terminal.

htop

Your terminal will be replaced by the htop interface. At the top, you will see meters for CPU, Memory (Mem), and Swap (Swp). Observe the Swp meter, which now shows the 2.00G of total swap space you configured.

  CPU[||                                 1.3%]   Tasks: 31, 0 thr; 1 running
  Mem[|||||||||                   78/1985MB]   Load average: 0.00 0.01 0.00
  Swp[                              0/2048MB]   Uptime: 00:05:10

This interface allows you to monitor how much of your physical and virtual memory is being used in real-time. To exit htop, simply press the q key on your keyboard.

Revert Changes and Restart VM

In this final step, you will learn how to disable and remove the swap file. This is useful when you no longer need the extra virtual memory or want to resize it.

First, you must disable the swap file using the swapoff command. This tells the operating system to stop using the file for swapping.

sudo swapoff ~/project/swapfile

Once the swap file is disabled, it is just a regular file, and you can safely delete it using the rm command.

sudo rm ~/project/swapfile

To confirm that the changes have been reverted, run the free -h command one last time.

free -h

The output should now be identical to what you saw in Step 1, with the Swap row showing zero capacity.

               total        used        free      shared  buff/cache   available
Mem:           3.5Gi       536Mi       1.1Gi       3.0Mi       1.9Gi       2.7Gi
Swap:             0B          0B          0B

You have successfully reverted the environment to its original state.

Summary

Congratulations on completing the lab! You have successfully learned how to manage virtual memory on a Linux system by using a swap file.

In this lab, you practiced several key system administration skills:

  • Checking system memory with free -h.
  • Creating a large file instantly with fallocate.
  • Setting up a swap area with mkswap.
  • Enabling and disabling swap space with swapon and swapoff.
  • Monitoring system resources with htop.

These skills are fundamental for managing Linux servers and ensuring they run smoothly, especially in resource-constrained environments.