Increase swap space in linux

How to increase swap space?

I have, by default, 250MB of swap space in Ubuntu, and I want to expand it to a larger size. I need 800MB, which I think will be enough to open several applications without having to hit the current limit of swap. I hope someone can help me.

If your swap partition can be expanded into adjacent unallocated space then use gparted . If such space is not available, perhaps because your active (booted) partition is using all the non-swap space, then gparted will not allow you to decrease the size of the active partition in order to create unallocated space. In this case you can re-install Ubuntu in order to create a larger swap space.

9 Answers 9

You can always create a swap file to add more swap space. This is not the same in every aspect as swap partition, but it will be easy and dynamic.

In the following steps, change /media/fasthdd/swapfile.img to anything you like. For example, it can be /swap.img as well. /media/fasthdd/swapfile.img is just an example filename. If you are using this one, then of course there must be a directory /media/fasthdd/ with enough free space for your new swap file.

Use any terminal application to run the commands of the following steps. All commands should be run with root privileges. To do this, you can either add sudo to the beginning of every command or run sudo bash before running the commands.

    Create an empty file: This file will contain virtual memory contents so make file big enough for your needs. This one will create a 1GiB file, which means +1GiB swap space for your system:

dd if=/dev/zero of=/media/fasthdd/swapfile.img bs=1024 count=1M 
mkswap /media/fasthdd/swapfile.img 
# Add this line to /etc/fstab /media/fasthdd/swapfile.img swap swap sw 0 0 
swapon /media/fasthdd/swapfile.img 

If everything goes well, you should see that more swap space is available for use. You can use the following commands to check your new swap and confirm that it is active:

$ cat /proc/swaps Filename Type Size Used Priority /media/fasthdd/swapfile.img file 8388604 2724 -1 $ grep 'Swap' /proc/meminfo SwapCached: 4772 kB SwapTotal: 8388604 kB SwapFree: 8355812 kB 

use the command «swapon -s» to see the status rather than «cat /proc/swaps». then the «free» command also gives swap usage info.

To edit /etc/fstab try this at the command line: sudo nano /etc/fstab .It is more safely, than sudo gedit /etc/fstab .

Resize Swap to 8GB

# Turn swap off # This moves stuff in swap to the main memory and might take several minutes sudo swapoff -a # Create an empty swapfile # Note that "1G" is basically just the unit and count is an integer. # Together, they define the size. In this case 8GB. sudo dd if=/dev/zero of=/swapfile bs=1G count=8 # Set the correct permissions sudo chmod 0600 /swapfile sudo mkswap /swapfile # Set up a Linux swap area sudo swapon /swapfile # Turn the swap on 

Check if it worked

Make it permanent (persist on restarts)

Add this line to the end of your /etc/fstab :

/swapfile swap swap sw 0 0 

/swapfile none swap sw 0 0 is preferable to this answer’s suggestion /swapfile swap swap sw 0 0 , as man fstab says (of the second field in an /etc/fstab line): «For swap partitions, this field should be specified as `none’.»

Читайте также:  Linux system call execute

Great answer. If anyone is wondering whether they should use 8GB as the swap file size, check out the recommendations in the SwapFaq. TLDR: You should be OK matching the swap size to the amount of RAM you have. I originally had 8GB of RAM but only 2GB of swap size, and I was experiencing regular crashes due to OOM, so I increased my swap to 8GB, exactly as per this answer. I’m sure that’s pretty common.

GUI method for increasing the size of swap partition

Another way to increase the swap size is to use the GParted partition Editor. In short, you resize the swap partition, then right click on it and choose «Swapon».

It’s easier to boot gparted-live-disk or an Ubuntu live disk (so that the all /dev/sda partitions will be unmounted). If you run Ubuntu live disk in some versions before 18.04, you may need to install gparted by running the commands below:

sudo apt-get update sudo apt-get install gparted && sudo gparted 

You must be able to increase the size of swap partition only if there is an unallocated space present before or after the swap partition. If there was no unallocated space (the space which we are trying to add with swap partition) below or above the swap partition, then we have to resize the partitions and get that unallocated space.

Case 1 — unallocated space present before or after the swap partition

GParted partitions

In the above screenshot, I had 11.4 GB of unallocated space before the linux-swap and 12.8 GB after the swap partition.

enter image description here

  • To resize, right click on the swap partition (/dev/sda9 here) and click on the Resize/Move option. It will look like this:
  • Dragging the slider arrows left or right then click on the Resize/Move button. Your swap partition will be resized.

Case 2 — unallocated space is between the partitions

enter image description here

In the above screenshot, the unallocated space which we wants to add to the swap partition was between dev/sda7 and /dev/sda8 .To move the unallocated space from that to just above swap partition,we have to follow the simple steps given below,

enter image description here

  • Right-click on the /dev/sda8 partition and click on Resize/Move option.
  • click and move the slider to the extreme left,so that the unallocated space which was just above the /dev/sda8 partition will comes below /dev/sda8 .After that click Resize/Move button.
  • And now the unallocated space was just above to the swap partition which was like Case 1.Now follow Case 1.

Case 3 — if the unallocated space was present outside the Extended partition)

enter image description here

I had an unallocated space of 18 GB just below to the extended partition.To add this space to the linux-swap partition(which was present inside the extended partition),we have to follow the below steps,

enter image description here

  • Right-click on the extended partition and select Resize/Move option,it will be like the below screenshot
  • Click and drag the arrow to the extreme right and click on Resize/Move,so that the unallocated space of 18 GB will comes at the bottom of extended partition.I had a 14.80 GB of unallocated space already present at the bottom and now the 18.34 GB combines with that to create unallocated space of (18.34+14.80 GB) at the bottom of the extended partition.
  • Now there was an unallocated space just below to the swap partition,it will be like Case 1,then follow case 1.
Читайте также:  Linux отобразить группы пользователей

NOTE: Don’t forget to take backup of all your important datas before proceeding the above operations.

Источник

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.

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:

Читайте также:  Посмотреть часовой пояс linux

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.

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.

Источник

How to increase the swap space in Linux

Linux Mint installation - SWAP

L inux is an open-source Unix-like operating system based on the Linux kernel. The Linux operating system has recently evolved to run on a wide variety of hardware, from phones, tablets, and supercomputers. Every Linux-based OS is packaged with a Linux Kernel that manages hardware resources and software packages that make up the operating system.

How do I increase swap space in Linux? Have you been looking for a way to increase the swap space on your Linux PC? If so, don’t worry since this article will cover all the aspects you need to know about increasing swap space in Linux.

Generally, every processor in the machine requires a certain amount of data capacity to run and store loaded applications. Therefore, all PCs are designed to have both primary and secondary memories from where all the programs can be run and stored information is executed.

For IT or computer scientist guys, you really need to take the storage issue seriously because your machine runs several exhaustive software that may lead to your machine crushing if not well managed.

There could be many issues related to storage capacity in the Linux system when running applications, some of which include adequate RAM and space to prevent machine crushing.

The Swap space in the Linux system helps the machine run in situations where the RAM is used up. This mainly occurs when the system is working in multiple applications at once, filling up the RAM thus, leaving no space for any other functionality. This, therefore, forces the system to begin utilizing the secondary storage where the Swap space is located.

The swap space’s access time is usually less than the system’s physical memory. When applications running stop working because of insufficient RAM space, they force inactive pages to move to the Swap space. Therefore, this means that if you have insufficient swap space, your machine will end up hanging or crushing.

Users who, for instance, use Video editing tools or memory-consuming applications should consider increasing their swap space for their machines to support the workload.

Now, most people find themselves in a dilemma on how much swap space is required in specific RAM size. However, since the article has created a guided table to cover and save you from this dilemma, don’t beat yourself up.

Источник

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