I have a Ubuntu 14.04 installation on my 32gb USB drive. I recently read that having a Swap causes a lot of reads and writes to the drive making the OS slow and reducing the lifespan of the drive. When I checked my RAM usage yesterday using free
, out of 4GB about 3GB was being used, and out of 1.5GB of Swap, 900MB was being used. Is it safe for me to just delete the swap partition?
Read below first!
Hi Ian, yes you could delete Swap with a live stick, but I don't recommend this. This may cause issues, depending on what OS you are using. There is a much nicer way to do that: Fire up a terminal (ctrl + alt + t), then type
replace hdb2 with your swap partition directory. You can get the directory by typing
So now your swap should be disabled. Then just remove the swap entry from /etc/fstab. You can do this by typing
Then hit enter and remove the line which extends the swap informations.
REBOOT!
After that you can easily delete the swap partition with gparted or a partition application of your choice. But I recommend you to first let it be there.
STOP, BEFORE YOU DO ANYTHING MENTIONED ABOVE, READ THIS: You are right, a swap partition produce much of traffic, but this might not make the OS slower. Another possibility instead of deleting the swap is to customize it: Fire up a terminal and run this:
Then add this line:
This customizes the swap, when it should be used. As lower the swapiness is, as less swap will be used. 10 is already extreme less. So swap will only be used in the worst case when there is absolutely no more RAM available.
That's it!
Sources:
How can I turn off swap permanently?
Removing Swap Space
Not really, no.
However, instead of disabling swappiness, you can run a script such as Zram to use compressed ram memory as swapspace instead of using a physical partition so that your system doesn't crash.
STEP ONE:
Execute the following command in an open terminal:
And copy/paste the following into the file:
Then, press CTRL + o to save the file and press CTRL + x to exit the file.
STEP TWO:
First, disable disk swappines:
Then, execute the following three commands in an open terminal to make the script executable, to start zram, and to make zram automatically start at boot:
STEP THREE:
Once you have enabled zram swap, you must edit /etc/fstab so that disk swappiness is not enabled at boot:
Locate the line that begins with:
And place a # at the beginning of the following line to change it from:
to this instead
example:
or you can delete the line instead.
Finally, press CTRL + o to save the file and press CTRL + x to exit the file.
No need to reboot. Also, you can verify zram is in use by executing the following command:
Sources:
https://wiki.debian.org/ZRam
https://code.google.com/p/compcache/
http://en.wikipedia.org/wiki/Zram
To explain:
While you may have 4GB of ram and may not be "using" a lot of ram for what you are running, linux uses up the remaining ram as cached ram to actually reduce disk activity by caching used applications to ram rather than having to page them from disk next time you open the application. When cached ram fills up ram or when an application needs to use the ram the cached data occupies, the system swaps out some of the cached ram to the swap partition to free up ram for applications in use and to cache more recently used applications in ram.
Also, some form of swappiness is necessary in order to hibernate and/or suspend.
Because of swappiness, your system is able to better utilize your available ram, reduce overall disk activity, and dramatically increase system responsiveness when under a heavy load all while preventing system crashes from maxed out ram.