How do I add a swap file?

Asked by tdn

How do I add a swap file to my system?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Seth Arnold
Solved:
Last query:
Last reply:
Revision history for this message
Best Seth Arnold (seth-arnold) said :
#1

(1) create the swap file with dd if=/dev/zero of=/path/to/swapfile bs=4096 count=$((256*<size in megabytes>))
(2) mark the file as a swapfile with mkswap /path/to/swapfile
(3) add the file to /etc/fstab (optional, if you want it on after rebooting..)
(4) swapon -a (if you edited fstab) or swapon /path/to/swapfile

There's nothing magic about the bs and count options to dd. Make sure that when you multiply them together, you get the size of the file you want. (Feel free to check it with ls -l.) But note that setting bs too small will take a long time, and setting it too large might put a lot of memory pressure on the system. (I wouldn't recommend trying to write out a whole gigabyte at once. :) I find it easy to think in kilobytes, so I often use 1024, but I imagine you'll want to add gigabytes of swap, and working in whole pages is usually more efficient.

Revision history for this message
Vikram Dhillon (dhillon-v10) said :
#2

Seth provided a really good answer :) keep up the good work. But I
would say that you only really need swap if your ram is less than say
1 gig. otherwise the swap won't do you much good :)

On Wed, Feb 17, 2010 at 6:40 AM, Seth Arnold
<email address hidden> wrote:
> Question #101400 on Ubuntu changed:
> https://answers.launchpad.net/ubuntu/+question/101400
>
>    Status: Open => Answered
>
> Seth Arnold proposed the following answer:
> (1) create the swap file with dd if=/dev/zero of=/path/to/swapfile bs=4096 count=$((256*<size in megabytes>))
> (2) mark the file as a swapfile with mkswap /path/to/swapfile
> (3) add the file to /etc/fstab (optional, if you want it on after rebooting..)
> (4) swapon -a (if you edited fstab) or swapon /path/to/swapfile
>
> There's nothing magic about the bs and count options to dd. Make sure
> that when you multiply them together, you get the size of the file you
> want. (Feel free to check it with ls -l.) But note that setting bs too
> small will take a long time, and setting it too large might put a lot of
> memory pressure on the system. (I wouldn't recommend trying to write out
> a whole gigabyte at once. :) I find it easy to think in kilobytes, so I
> often use 1024, but I imagine you'll want to add gigabytes of swap, and
> working in whole pages is usually more efficient.
>
> --
> You received this question notification because you are an answer
> contact for Ubuntu.
>

--
Regards,
Vikram Dhillon

~~~
There are lots of Linux users who don't care how the kernel works, but
only want to use it. That is a tribute to how good Linux is.
-- Linus Torvalds

Revision history for this message
tdn (spam-thomasdamgaard) said :
#3

Seth, thanks.
I would like to add, that for adding the swap file to fstab, you need to add this line:
/swap/swapfile.swap none swap sw 0 1

Vikram, some people need more than one gig of RAM. I wanted swap, because I experienced out of memory events (oom-killer).

Revision history for this message
tdn (spam-thomasdamgaard) said :
#4

Thanks Seth Arnold, that solved my question.