Add or edit Swap Space for Linux

Introduction

To run and store loaded applications, each processor in the machine needs a certain quantity of data storage space. Every PC is built with primary and secondary memories, from which applications may operate, and data can be stored while processing. If you work in IT or are a computer scientist, you should give storage some severe thought. In that circumstance, some software programs must be developed.

Why you Need Swap Space

Linux systems may experience various storage-related problems; nonetheless, you must have enough RAM to run applications.

When RAM is going to run out, the Linux system’s Swap feature is helpful. If RAM runs out of room when running numerous apps, the system switches to using Swap, the system’s secondary storage, to continue operating.

Swap space has a faster access time than the system’s actual memory. Static pages were compelled to relocate to swap space when running apps ceased functioning due to a lack of RAM capacity. Use swap space if you’re using memory-intensive software or the tools for video editors.

The topic of how much swap space is needed for a specific RAM may now cross your thoughts. Do not be concerned; a reference table is mentioned here:

System RAM   Recommended Swap Space
Less than 2 GB   2 x RAM
2 GB – 8 GB   1 x RAM
8 GB – 64 GB   0.5 x RAM
More than 64 GB   Depend on workload

The suggested action for CentOS and Red hat would be:

System RAMRecommended Swap Space
Less than 2 GB2 x RAM
2 GB – 8 GB1 x RAM
8 GB – 64 GBMin 4 GB
More than 64 GBMin 4 GB

Additionally, the advice is almost unchanged for the Ubuntu operating system:

System RAMRecommended Swap Space
Less than 1 GBSwap >= RAM < 2xRAM
Greater than 1 GBSwap >= square root of RAM (but) Swap < 2xRAM

How to Increase Swap Space in Linux

We must first determine whether swap space is enabled before expanding swap space on the Linux system. Type the following in the terminal to verify it:

sudo swapon --show

If you received no output, your system doesn’t currently have any open swap space.

Another option is to use the “free -h” command to view the memory and swap space details.

free -h

Write the swap space command on the terminal:

sudo dd if=/dev/zero of=/swap_file bs=4GB count=1

in this example our ram is 4GB so Consequently, the memory designated for Swap would be about 4 GB.

By the specifications, you can set the value of bs and count.

  • Bs: sets of block size
  • Count: number of blocks

Now, change the users’ permission access to 600 to prevent people from reading crucial data from the swap file:

sudo chmod 600 /swap_file

Utilize the “mkswap” command utility to enable the swap area on the file “swap file”:

sudo mkswap /swap_file

The command to use to enable the swap file “swap file” is as follows:

sudo swapon /swap_file

To determine whether swap space has grown, run the “free -h” command:

free –h

Conclusion:

Every CPU on the system needs RAM to operate numerous programs and tools. Although the system is always allotted memory or RAM, there are times when this is insufficient to support innumerable applications running at once.

Swap space is stored in the system as secondary memory in a Linux system. Applications can be run in swap space to supplement RAM when it is full.

During the Linux distribution installation procedure, swap space is allotted. However, it can be modified later if necessary.

Leave a Reply

Your email address will not be published. Required fields are marked *