17

I installed Ubuntu a few weeks ago and I usually run more than 6 programs(sts,web browsers, and so on).

The system is getting stuck.

My RAM size is 6 GB and swap 2 GB. I decided to increase swap size to 15 GB. When I was trying to accomplish this work, I got the following error. I have linked to the tutorial that I was following.

sudo swapon --show
[sudo] password for decoders: 
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -1
decoders@10decoders:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           5.7G        1.1G        3.4G        217M        1.1G        4.1G
Swap:          2.0G          0B        2.0G
decoders@10decoders:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.9G     0  2.9G   0% /dev
tmpfs           583M  9.0M  574M   2% /run
/dev/sda1       293G   14G  265G   5% /
tmpfs           2.9G  4.7M  2.9G   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           2.9G     0  2.9G   0% /sys/fs/cgroup
tmpfs           583M  144K  583M   1% /run/user/1000
decoders@10decoders:~$ sudo fallocate -l 15G /swapfile
fallocate: fallocate failed: Text file busy
decoders@10decoders:~$ ls -lh /swapfile
-rw------- 1 root root 2.0G May 19 11:43 /swapfile
decoders@10decoders:~$ 

I followed this tutorial link: http://www.tutorialspoint.com/articles/how-to-increase-swap-space-on-linux-ubuntu

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
  • 6
    You can't fallocate a swapfile that's currently in use. You'd have to at least do a swapoff -a first. Secondly, why do you think that increasing swap would solve your problem when your current swapon command shows no swap usage? Lastly, setting is to 15G is excessive. Figure on 6G... 12G max. – heynnema May 31 '17 at 15:41
  • 1
    now i do not have swap usage. When i run more than five programs, I see swap usage. what is swapoff -a? – Kumaresan Perumal Jun 01 '17 at 04:59
  • 6
    swapoff -a turns off swap usage. You were trying to allocate 15G to /swapfile, but you can't do that if it's in use. You do a swapoff -a, fallocate (you may have to delete the current /swapfile first), mkswap /swapfile, then swapon -a. If you're not sure what you're doing... then don't do it. – heynnema Jun 01 '17 at 12:50
  • @heynnema please tell me the procedure to accomplish the work. – Kumaresan Perumal Jun 01 '17 at 12:52
  • 1
    See https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04. Don't do the vm.swappiness or cache pressure parts. – heynnema Jun 01 '17 at 13:35
  • if i increase swapfile size. will it resolve my computer slowness problem? – Kumaresan Perumal Jun 01 '17 at 13:42
  • Create a 6G /swapfile. It may or may not help with slowness. Use the top command to see if there's a process consuming too much CPU time. See man top for more info. – heynnema Jun 01 '17 at 13:45

1 Answers1

29

Creating a SWAP partition in Ubuntu, Linux Mint and derivatives


METHOD 1: Command-line Way from Terminal (Fastest way!)

STEP 1: First step is to check if by chance is there any SWAP partition already created in your PC:

sudo swapon --show

Enter your root password. If you see no output, it means that the SWAP doesn’t exist.

STEP 2: Next, let’s see the current partition structure of your computer’s hard disk:

df -h

STEP 3: As heynnema commented, before you start the changes disable the use of swap:

sudo swapoff -a

STEP 4: Now it's time to create the SWAP file. Make sure you have enough space on the hard disk. It is a matter of preference in how much SWAP size you need.

My suggestion is: If you have a maximum of 4GB of RAM I would suggest putting twice the RAM for the SWAP (8GB for SWAP). For PCs with more than 4GB I recommend the same number of RAM for SWAP plus 2GB. Example: In my case it's 8GB, I put 8GB + 2GB, totaling 10GB for SWAP. But you may feel free to make your choice.

sudo dd if=/dev/zero of=/swapfile bs=1G count=10 status=progress

STEP 5: SWAP file is now created. Let’s give root-only permissions to it.

sudo chmod 600 /swapfile

STEP 6: Mark the file as SWAP space:

sudo mkswap /swapfile

STEP 7: Finally enable the SWAP.

sudo swapon /swapfile

STEP 8: You can now check using the same swapon command to check if SWAP is created.

sudo swapon --show

STEP 9: Also check the final partition structure again.

free -h

STEP 10: Once everything is set, you must set the SWAP file as permanent, else you will lose the SWAP after reboot. Run this command:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Finished, now exit the terminal!

You can check SWAP status on System Monitor utility.


METHOD 2: GUI Way using GParted

If you want to go directly through the graphical interface, enter the reference link below that is well explained.


Reference:

Manuel Jordan
  • 1,768
  • 7
  • 30
  • 50