Change swap size in linux

How to Change Swap File Size in Ubuntu

In this article, we will discuss what a swap file is, its types, and how to change the swap file size in Ubuntu. This is particularly useful for users who want to optimize their system performance by resizing the swap file based on their system’s requirements.

What is Swap?

Swap is a virtual memory management technique used by Linux systems, including Ubuntu, to temporarily store data that is not currently in use by the RAM. When the physical memory (RAM) is not sufficient to handle all the running processes, the operating system moves some data to the swap space, thereby freeing up RAM for other processes. Swap space can be created on the hard drive in the form of a swap file or a swap partition.

Types of Swap

There are two types of swap spaces in Linux systems:

  • Swap Partition: This is a dedicated partition on the hard drive created during the installation of the operating system. It is generally faster than a swap file and is recommended for systems with a high demand for swap space.
  • Swap File: This is a file created on an existing file system. It is simpler to manage and resize compared to a swap partition. Swap files are suitable for systems with lower swap space requirements or systems with limited disk space.

In this article, we will focus on resizing a swap file in Ubuntu.

Disable Existing Swap

Before resizing the swap file, you must first disable the existing swap. Follow these steps to disable the swap:

Open the terminal by pressing Ctrl + Alt + T or searching for “Terminal” in the application menu.

To disable the swap, enter the following command:

Create and Use a New Swap File

To create a new swap file with a different size, follow the steps below:

    Remove the existing swap file:

sudo fallocate -l 4G /swapfile 
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 

Источник

How Do I Increase Swap Space in Linux

Every processor in the machine needs an amount of data capacity to run and store loaded applications. All the PCs are designed to have primary and secondary memories from where programs can run, and information is being stored while executing.

You need to think about storage seriously if you belong to IT or working as a computer scientist. In that case, bundles of software packages are required to work on.

There could be many issues related to storage capacity in Linux systems to run applications; you need adequate RAM; otherwise, the application will crash.

Читайте также:  Database system for linux

The Swap in the Linux system helps when RAM is about to exhaust. While working with multiple applications, if RAM fills up and there is no space to function, the system starts utilizing the secondary storage where Swap is located.

The access time of swap space is less than the system physical memory. When running applications stopped working because of insufficient space in RAM, inactive pages are forced to move towards swap space. If you’re using the video editor tools or memory-consuming applications, it would be good to use swap space.

Now, the question may arise in your mind that how much swap space is required for a particular RAM. Do not worry about it; a guide 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

For Centos and Redhat, the recommendation would be:

System RAM Recommended Swap Space
Less than 2 GB 2 x RAM
2 GB – 8 GB 1 x RAM
8 GB – 64 GB Min 4 GB
More than 64 GB Min 4 GB

And for the Ubuntu system, the recommendation is somehow little changed:

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

How to Increase Swap Space In Linux

To increase swap space in the Linux system, we first need to check if swap space is enabled.

To check it, type in the terminal:

If you didn’t get any output, it means your system doesn’t have swap space available at the moment.

There is another way to check the memory and swap space information i-e using the “free -h” command.

In my case, RAM is less than 2 GB, so the allocated swap space memory would be around 1 GB (as we have mentioned in the recommended cases above).

(Note that all of the below-mentioned commands required sudo privileges to make changes).

In the terminal, write down the command to increase swap space:

You can set the value of bs and count according to the requirement.

Keep in mind:

Now, set the permission access for the users as 600 so users couldn’t be able to read important data from the swap_file:

To enable the swap area on the file “swap_file,” use the “mkswap” command utility:

The next step is to enable the swap file “swap_file” using the command:

Run the “free -h” command to check if swap space has increased:

So, when we checked before, the swap space was 923 MB. And now it has been updated to 1.8 GB.

Conclusion:

While using the system, every processor requires memory to run tons of packages and tools. There is always allocated memory in the system, i-e, RAM, but sometimes it is not enough for multiple applications to run simultaneously.

Читайте также:  Отключить bluetooth kali linux

In a Linux system, swap space is located in the system as secondary memory. When RAM is exhausted, swap space helps to run applications in it.

We allocate swap size during the installation process of Linux distribution. But it can be changed later according to requirement.

The guide has shown how we can increase swap space in Linux Distributions.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

Change Swap Size in Ubuntu

In the case of Linux, the swap file is an important part. It’s not a must-have option but having one is crucial for a number of purposes. Generally, when installing the system, you have to declare the size of the swap file. The recommended size is 4GB but you can either decrease or increase it depending on your demand. Today, let’s have a look at changing the swap size in Ubuntu.

Note – Running all these commands require root privilege, so I recommend running a “root” terminal.

Finding out available swap file(s)

Before we get to change the swap, let’s find out how much swap size we have.

According to the result, the system has a swap file at “/swapfile”.

For manipulating the swap file, we have to disable it first.

Now, change the size of the swap file –

Here, the total size of the swap file will be bs*count = 1M x 4096 = 4GB

Make the “/swapfile” usable again –

After restarting your system, check out the result –

Deleting the swap file

If your RAM space is high enough, then you probably want to get rid of the swap file. If that’s your case, then let’s get rid of the swap file!

Don’t worry! I’ll also show you how to set a completely fresh swap file.

For deleting the swap file, run the following commands –

Remove the swap file entry from “/etc/fstab” file. It should be the last line of the file.

Now, time to remove the actual swap file.

Don’t forget to restart your system!

For getting back the swap file, use the following commands –

# Adding permission where only root user can read the swap file

# Setup the file for swap space

Edit the “/etc/fstab” file and add the following line at the end of the file –

Voila! Your swap file is back!

What if you had a swap partition? You have to perform actions on the partition itself. That’s more troublesome than just simply managing your swap files directly using the methods above, right?

I recommend deleting the “swap” partition and use the above method to create a new swap file.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Читайте также:  Postgresql linux смена пароля

Источник

How to Increase Swap Size on Ubuntu Linux

In this quick tip, you’ll learn to increase the swap size on Ubuntu and other Linux distributions.

The recent releases of Ubuntu use swap file instead of the traditional swap partition. The swap file is simply a file under the root which is used as swap to share the burden on the RAM.

The biggest advantage of using a swap file is that you can easily resize it. That’s not always the case when you use a dedicated swap partition.

Let’s see how to resize the swap space on Ubuntu.

Increase swap size on Ubuntu

If you are using swap partition and want to increase the swap size, you can create swap file. Your Linux system can use multiple swap spaces as needed. This way, you won’t have to touch the partition.

This tutorial assumes that you are using swap file on your system, not a swap partition.

Now, let’s see how to increase the swap file. First thing first, make sure that you have a swap file in your system.

It will show the current swap available. If you see the type file, it indicates that you are using a swap file.

swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2

Now before you resize the swap file, you should turn the swap off. You should also make sure that you have enough free RAM available to take the data from swap file. Otherwise, create a temporary swap file.

You can disable a given swap file using this command. The command doesn’t produce any output and it may take a few minutes to complete:

Now use the fallocate command in Linux to change the size of the swap file.

sudo fallocate -l 4G /swapfile

Make sure that you mark this file as swap file:

You should see an output like this where it warns you that old swap signature is being wiped out.

sudo mkswap /swapfile mkswap: /swapfile: warning: wiping old swap signature. Setting up swapspace version 1, size = 4 GiB (4294967296 bytes) no label, UUID=c50b27b0-a530-4dd0-9377-aa28eabf3957

Once you do that, enable the swap file:

That’s it. You just increased the swap size in Ubuntu from 2 GB to 4 GB. You can check swap size using the free command or the swapon —show command.

free -h total used free shared buff/cache available Mem: 7.7G 873M 5.8G 265M 1.0G 6.3G Swap: 4.0G 0B 4.0G

You see how easy it is to resize swap size thanks to the swap files. You didn’t touch the partition, you didn’t reboot the system. Everything was done on the fly. How cool is that!

Similarly, resizing LVM is also a painless task. I like how these things have improved the Linux experience.

I hope you found this quick tutorial helpful in resizing the swap space on Ubuntu as well as other Linux distributions. If you have questions or suggestions, please leave a comment below.

Источник

Оцените статью
Adblock
detector