swap on the fly on any linux system

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
sickgut
Posts: 1156
Joined: Tue 23 Mar 2010, 19:11
Location: Tasmania, Australia in the mountains.
Contact:

swap on the fly on any linux system

#1 Post by sickgut »

Hi Dogfarmers,

i would like to share with yall a trick i use all the time that most likely the techy people among you already know but those of you who dont, this is really cool to be able to do.

These commands are available in any linux, so it should be universal.

open a terminal, most linuxs have a console/ terminal icon you can click to do this, you dont have to exit your desktop environment, this works within X or at the console, it dont matter.

use the following (lines separated by enter):

------- code snippet ---------
cd /
dd if=/dev/zero of=mrswappy bs=1M count=300
mkswap mrswappy
swapon mrswappy

------- code snippet ---------

when you cd / you are moving to the top dir of your OS, this is usually located on the hdd and partition the OS is using unless your using a live OS from a pen drive or cdrom or some frugal setups.

the dd if=/dev/zero of=mrswappy bs=1M count=300 command creates a new file that contains all zeros. Note that the name mrswappy can be anything you like and the count can be anything you like. the above example makes a 300MB swapfile. You can change the count to any other number you like, 1000 will make a 1000MB swap, and 50 will make a 50MB swap etc... etc...

mkswap mrswappy command turns the file mrswappy into a swapfile, not just an empty file, altho yes it is empty still, this command turns it into a filesystem that is the same as the linux swap partition, it even had a uuid and can have a drive label if you want it to altho that wont be discussed here.

swapon mrswappy command tells the linux system to mount the new swap file and use it immediately.

You can see your swapfile in action by the: free and top commands
free command will show you the used ram and swap etc.
Top will show you in real time the usage of the ram and swap and what resources all the apps that are running are using. The ram and swap stuff is at the top of the screen, application resource stuff is below this.

remember the file is located in the / dir and you can delete it next boot, it will not be automatically activated next time you boot. If you want it to be automatically actuvated then on boot then add an entry in /etc/fstab ... you may wanna google how to do this, i wont explain it here as the point of this post is to show you how to mount it on the fly whenever you like not make a permanent change.

Note for live and or frugal installed OSes:

instead of using cd / at the beginning, cd to a dir that you know accesses either the HDD or partition that you know is a real HDD or partition that is outside of your savefile or the live temporary ramdisk.

You can use the command: fdisk -l (lowercase L not an i or upper case i)

this will list the connected disks and you can mount whatever you like and make the swapfile there, it can be a usb stick or external hdd or another hdd or partition but not in a savefile (wont work) or in the ramdisk (defeats the purpose).

When trying a live cd/ dvd and you have low ram this can help quite alot. usually there will be a /dev/sda1 on your system you can mount then cd to and create the swap file on. To make sure your using a real disk in a live Puppy then open up the disk via the icon for it on the desktop then right click within the disk window and select "terminal here" then run all the commands except for the cd / at the beginning, as the "terminal here" does this automatically, it cd's to the filesystem you right clicked in.

2 ways to remove the swapfile after you have finnished with it:

1) reboot and delete the file without mounting it first
2) no need to reboot, but run swapoff -a (can also be swapoff all) and then delete the swapfile. However in some cases you may have used too many resources to be able to swapoff. If this is the case you cant delete it as its in use, so you will need to reboot first then delete as per option 1.

This is especially useful if you rent a cheap virtual private server (vps) that uses Xen virtualization, as this allows you to run your own swap. Even tho it probable already has a small mount of swap on another partition, you can add more. (openvz doesnt let you do this). When you add more swap on a VPS often its as good as RAM, as the host system unless its overloaded will be running all its virtualization within its RAM, so your new swap file is possibly as fast as your ram is. However this is a little bit of a crap shoot as if the host server is oversold or under a heavy load at the time then when your swap is activated and used it will be using HDD space not the RAM of the host server.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Very informative article. Thanks.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#3 Post by starhawk »

Just did this for my little Fujitsu thin client (which, given that it maxes out at a half gig of RAM, should perhaps be called a "FuJUNKY"! :lol: )

This should be stickied, it's handy!

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#4 Post by mikeb »

Yep...swap files are often forgotten and noobs are told to repartition their windows boxes which can really screw things up.

With the 2.4 kernel there was a little daemon that would create and remove swap files as needed checking for how full the ram was...just like windows has always done...neat..

Unfortunately its disappeared off the scene but methinks a cute script could do the job once a foolproof way of checking ACTUAL ram usage was found.

mike

starhawk
Posts: 4906
Joined: Mon 22 Nov 2010, 06:04
Location: Everybody knows this is nowhere...

#5 Post by starhawk »

mikeb wrote:checking ACTUAL ram usage
# free :?:

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#6 Post by mikeb »

free says 510204 502684 7520
-/+ buffers/cache: 225064 285140

htop says 221MB so i suppose there is some agreement there if you use the second line..... the rest is just polling and creating/deleting as required...perhaps in say 256MB blocks.

mike

Post Reply