Skip to contentSkip to conversation

Swap partition vs swap file

DISCUSSION Mike Richards 12 participants 19 messages Jun 29, 2005 – Jul 13, 2005

lore
Share & tools
lore
#0 lore raw

Is there any significant difference these days between a swap partition and a swap file?

An exhaustive Google search turns up several conflicting answers. The consensus seems to be that a swap partition is more efficient than a swap file, but whether or not the difference is noteworthy is never definitively answered.

For the sake of argument, let's assume you've got modern hardware with ample RAM and a recent kernel (a late 2.4.x or 2.6.x), and that under normal conditions you never seeing more than 50-100MB of swap used.

Given this situation, is there any significant performance or stability advantage to using a swap partition instead of a swap file?

#1 lore raw
Quoted 3 lines · Mike Richards

Given this situation, is there any significant performance or
stability advantage to using a swap partition instead of a swap file?

In 2.6 they have the same reliability and they will have the same performance unless the swapfile is badly fragmented.

#2 lore raw

Hmm, but what about tells mkswap(8) tells:
"...Note that a swap file must not contain any holes (so, using cp
(1) to create the file is not acceptable)..."?

If swap file will be fragmented it will contain holes.
Or what type of holes can cp(1) can create, which are not usefull for mkswap(8)?

2005/6/29, Andrew Morton <[email protected]>:

Quoted 8 lines
Quoted 3 lines · Mike Richards

Given this situation, is there any significant performance or
stability advantage to using a swap partition instead of a swap file?

In 2.6 they have the same reliability and they will have the same performance unless the swapfile is badly fragmented.

#3 lore raw
Quoted 5 lines · Marat Buharov

Hmm, but what about tells mkswap(8) tells:
"...Note that a swap file must not contain any holes (so, using cp
(1) to create the file is not acceptable)..."?

If swap file will be fragmented it will contain holes.

No, holes are actual missing blocks (which are read as zeros) while fragmentation just means the blocks of the file are not contiguous on disk.

-Doug

#4 lore raw
In reply to Andrew Morton
Quoted 5 lines
Quoted 2 lines

Given this situation, is there any significant performance or
stability advantage to using a swap partition instead of a swap file?

In 2.6 they have the same reliability and they will have the same performance unless the swapfile is badly fragmented.

Thanks for the reply -- that's been bugging me for a while now. There are a lot of different opinions on the net, and most of the conventional wisdom says use a partition instead of a file. It's nice to hear from an expert on the matter.

Three more short questions if you have time:

1. You specify kernel 2.6 -- What about kernel 2.4? How less reliable or worse performing is a swapfile on 2.4?

2. Is it possible for the swapfile to become fragmented over time, or does it just keep using the same blocks over and over? i.e. if it's all contiguous when you first create the swapfile, will it stay that way for the life of the file?

3. Does creating the swapfile on a journaled filesystem (e.g. ext3 or reiser) incur a significant performance hit?

Thanks again. It's much appreciated.

#5 lore raw
Quoted 12 lines · Mike Richards
Quoted 6 lines
Quoted 3 lines

Given this situation, is there any significant performance or
stability advantage to using a swap partition instead of a swap file?

In 2.6 they have the same reliability and they will have the same performance unless the swapfile is badly fragmented.

Thanks for the reply -- that's been bugging me for a while now. There are a lot of different opinions on the net, and most of the conventional wisdom says use a partition instead of a file. It's nice to hear from an expert on the matter.

Quoted 4 lines

Three more short questions if you have time:

1. You specify kernel 2.6 -- What about kernel 2.4? How less reliable or worse performing is a swapfile on 2.4?

2.4 is weaker: it has to allocate memory from the main page allocator when performing swapout. 2.6 avoids that.

Quoted 4 lines

2. Is it possible for the swapfile to become fragmented over time, or does it just keep using the same blocks over and over? i.e. if it's all contiguous when you first create the swapfile, will it stay that way for the life of the file?

The latter. Create the swapfile when the filesystem is young and empty, it'll be nice and contiguous. Once created the kernel will never add or remove blocks. The kernel won't let you use a sparse file for a swapfile.

Quoted 2 lines

3. Does creating the swapfile on a journaled filesystem (e.g. ext3 or reiser) incur a significant performance hit?

None at all. The kernel generates a map of swap offset -> disk blocks at swapon time and from then on uses that map to perform swap I/O directly against the underlying disk queue, bypassing all caching, metadata and filesystem code.

#6 lore raw
Quoted 21 lines · Andrew Morton
Quoted 12 lines · Mike Richards
Quoted 6 lines
Quoted 3 lines

Given this situation, is there any significant performance or
stability advantage to using a swap partition instead of a swap file?

In 2.6 they have the same reliability and they will have the same performance unless the swapfile is badly fragmented.

Thanks for the reply -- that's been bugging me for a while now. There are a lot of different opinions on the net, and most of the conventional wisdom says use a partition instead of a file. It's nice to hear from an expert on the matter.

Quoted 4 lines

Three more short questions if you have time:

1. You specify kernel 2.6 -- What about kernel 2.4? How less reliable or worse performing is a swapfile on 2.4?

2.4 is weaker: it has to allocate memory from the main page allocator when performing swapout. 2.6 avoids that.

Quoted 6 lines
Quoted 4 lines

2. Is it possible for the swapfile to become fragmented over time, or does it just keep using the same blocks over and over? i.e. if it's all contiguous when you first create the swapfile, will it stay that way for the life of the file?

The latter. Create the swapfile when the filesystem is young and empty,

I guess/hope dd always makes it contiguously.

Quoted 3 lines

it'll be nice and contiguous. Once created the kernel will never add or remove blocks. The kernel won't let you use a sparse file for a swapfile.

Signature · 2 lines
Coywolf Qi Hunt
http://ahbl.org/~coywolf/
#7 lore raw
Quoted 1 line · In article <[email protected]> you wrote:

I guess/hope dd always makes it contiguously.

No, it is creating files by appending just like any other file write. One could think about a call to create unfragmented files however since this is not always working best is to create those files young or defragment them before usage.

Gruss
Bernd

#8 lore raw
Quoted 4 lines · Bernd Eckenfels

No, it is creating files by appending just like any other file write. One could think about a call to create unfragmented files however since this is not always working best is to create those files young or defragment them before usage.

Except that this defeats one of the biggest advantages a swap file has over a swap partition: the ability to easilly reconfigure the amount of hd space reserved for swap.

Signature · 1 line
Jeremy Nickurak <[email protected]>
#9 lore raw
Quoted 9 lines · Jeremy Nickurak
Quoted 4 lines · Bernd Eckenfels

No, it is creating files by appending just like any other file write. One could think about a call to create unfragmented files however since this is not always working best is to create those files young or defragment them before usage.

Except that this defeats one of the biggest advantages a swap file has over a swap partition: the ability to easilly reconfigure the amount of hd space reserved for swap.

You can still reconfigure the amount of swap by creating more swapfiles later - you merely risk a fragmented file. Keep your filesystems only half full though, and it won't be a big problem.
:-)

Helge Hafting

#10 lore raw
In reply to Jeremy Nickurak
Quoted 9 lines · Jeremy Nickurak
Quoted 4 lines · Bernd Eckenfels

No, it is creating files by appending just like any other file write. One could think about a call to create unfragmented files however since this is not always working best is to create those files young or defragment them before usage.

Except that this defeats one of the biggest advantages a swap file has over a swap partition: the ability to easilly reconfigure the amount of hd space reserved for swap.

Of course, now this begs the question: Is it possible to create a large file w/o actually writing that much to the device (ie uninitialized). There's absolutely no reason that a swap file needs to be fully initialized, only part which mkswap does. Of course, I would expect that ONLY root beable to do this. (or capsysadmin or whatever the caps are)

Signature · 1 line
 Lab tests show that use of micro$oft causes cancer in lab animals
#11 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 11 lines · Wakko Warner
Quoted 9 lines · Jeremy Nickurak
Quoted 4 lines · Bernd Eckenfels

No, it is creating files by appending just like any other file write. One could think about a call to create unfragmented files however since this is not always working best is to create those files young or defragment them before usage.

Except that this defeats one of the biggest advantages a swap file has over a swap partition: the ability to easilly reconfigure the amount of hd space reserved for swap.

Quoted 5 lines

Of course, now this begs the question: Is it possible to create a large file w/o actually writing that much to the device (ie uninitialized). There's absolutely no reason that a swap file needs to be fully initialized, only part which mkswap does. Of course, I would expect that ONLY root beable to do this. (or capsysadmin or whatever the caps are)

That would make the swap file fragment as it's used, instead of allocating one big file (the entire file) at once (and hopefully get one contiguous chunk of the disk).

- -sandalle

- --

Eric Sandall                     |  Source Mage GNU/Linux Developer
[email protected]                  |  http://www.sourcemage.org/
http://eric.sandall.us/          |  SysAdmin @ Inst. Shock Physics @ WSU
http://counter.li.org/  #196285  |  http://www.shock.wsu.edu/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFC0FbfHXt9dKjv3WERApKjAJ9ZObnrYWCmTyZW0ChggtgGjTKIvQCfbnvm /U4zfjTYqMxEd5vmIRe1wbM=
=smCj
-----END PGP SIGNATURE-----

#12 lore raw
Quoted 10 lines · Eric Sandall
Quoted 6 lines

Of course, now this begs the question: Is it possible to create a large file
w/o actually writing that much to the device (ie uninitialized). There's absolutely no reason that a swap file needs to be fully initialized, only part which mkswap does. Of course, I would expect that ONLY root beable to do this. (or capsysadmin or whatever the caps are)

That would make the swap file fragment as it's used, instead of allocating one big file (the entire file) at once (and hopefully get one contiguous chunk of the disk).

You misunderstood entirely what I said.

Signature · 1 line
 Lab tests show that use of micro$oft causes cancer in lab animals
#13 lore raw
Quoted 1 line · In article <[email protected]> you wrote:

You misunderstood entirely what I said.

There is no portable/documented way to grow a file without having the file system null its content. However why is that a problem, you dont create those files very often. Besides it is better for the OS to be able to asume that a page with zeros in it is equal to the page on fresh swap.

Gruss
Bernd

#14 lore raw
Quoted 7 lines · Bernd Eckenfels
Quoted 1 line · In article <[email protected]> you wrote:

You misunderstood entirely what I said.

There is no portable/documented way to grow a file without having the file system null its content. However why is that a problem, you dont create those files very often. Besides it is better for the OS to be able to asume that a page with zeros in it is equal to the page on fresh swap.

So are you saying that if I create a swap partition it's best to use dd to zero it out before mkswap? If no, then why would a file be different? I know there's no documented way to create a file of given size without writing content. I saw windows grow a pagefile several meg in less than a second so I'm sure that it doesn't zero out the space first.

As far as portable, we're talking about linux, portability is not an issue in this case. I myself don't use swap files (or partitions), however, there was a project I recall that would dynamically add/remove swap as needed. Creating a file of 20-50mb quickly would have been beneficial.

Signature · 1 line
 Lab tests show that use of micro$oft causes cancer in lab animals
#15 lore raw
Quoted 2 lines · In article <[email protected]> you wrote:

So are you saying that if I create a swap partition it's best to use dd to zero it out before mkswap?

Nope I did not. However I dont know of any other shell tool which can do it that easyly.

Quoted 2 lines

As far as portable, we're talking about linux, portability is not an issue in this case.

Portability across Filesystems.

Gruss
Bernd

#16 lore raw
In reply to Wakko Warner
Quoted 9 lines · Wakko Warner
Quoted 6 lines · Bernd Eckenfels
Quoted 3 lines · In article <[email protected]> you wrote:

You misunderstood entirely what I said.

Quoted 6 lines
Quoted 6 lines

There is no portable/documented way to grow a file without having the file system null its content. However why is that a problem, you dont create those files very often. Besides it is better for the OS to be able to asume that a page with zeros in it is equal to the page on fresh swap.

You don't need to zero out swapfiles. You can fill them with anything, even /dev/urandom. Zero-filling may be faster though. A swapfile is not zero the second time you use it - then it contains leftovers from last time.

Quoted 8 lines

So are you saying that if I create a swap partition it's best to use dd to zero it out before mkswap? If no, then why would a file be different? I know there's no documented way to create a file of given size without writing content. I saw windows grow a pagefile several meg in less than a second so I'm sure that it doesn't zero out the space first.

Linux doesn't grow swapfiles at all. It uses what's there at mkswap time. You can make new ones of course - manually.

Quoted 5 lines

As far as portable, we're talking about linux, portability is not an issue
in this case. I myself don't use swap files (or partitions), however, there
was a project I recall that would dynamically add/remove swap as needed. Creating a file of 20-50mb quickly would have been beneficial.

You can create 50M quickly - even if it actually have to be written. If you can't, don't use that device for swap.

Ability to allocate some blocks without actually writing to them is nice for this purpose, but current linux filesystems doesn't have an api for doing that. The necessary changes would touch all existing writeable filesystems, and that is a lot of work for very little gain. As they say, you don't create swapfiles all that often. The time saved on swapfile creation might take a long time to make up for the time spent on making, auditing and supporting those changes.

Helge Hafting

#17 lore raw
Quoted 5 lines · Helge Hafting

Wakko Warner wrote:
You don't need to zero out swapfiles. You can fill them with anything, even /dev/urandom. Zero-filling may be faster though. A swapfile is not zero the second time you use it - then it contains leftovers from last time.

I understand this part.

Quoted 8 lines
Quoted 5 lines

So are you saying that if I create a swap partition it's best to use dd to zero it out before mkswap? If no, then why would a file be different? I know there's no documented way to create a file of given size without writing content. I saw windows grow a pagefile several meg in less than a second so I'm sure that it doesn't zero out the space first.

Linux doesn't grow swapfiles at all. It uses what's there at mkswap time. You can make new ones of course - manually.

And this part. I've never known linux to grow the swap file. I did try the sparse one a long time ago. Of course it didn't work.

Quoted 8 lines
Quoted 5 lines

As far as portable, we're talking about linux, portability is not an issue in this case. I myself don't use swap files (or partitions), however, there
was a project I recall that would dynamically add/remove swap as needed. Creating a file of 20-50mb quickly would have been beneficial.

You can create 50M quickly - even if it actually have to be written. If you can't, don't use that device for swap.

Not all systems can create 50mb in a short time. Especially when the system/device is under load. Not all systems have multiple disks either.

Quoted 10 lines

Ability to allocate some blocks without actually writing to them is nice for this
purpose, but current linux filesystems doesn't have an api for doing that. The necessary changes would touch all existing writeable filesystems, and that is a lot of work for very little gain. As they say, you don't create swapfiles
all that often. The time saved on swapfile creation might take a long time to
make up for the time spent on making, auditing and supporting those changes.

I hadn't considered this "portability" so I didn't understand at that point.

Signature · 1 line
 Lab tests show that use of micro$oft causes cancer in lab animals
#18 lore raw
Quoted 5 lines
Quoted 2 lines

Linux doesn't grow swapfiles at all. It uses what's there at mkswap time. You can make new ones of course - manually.

And this part. I've never known linux to grow the swap file. I did try the sparse one a long time ago. Of course it didn't work.

I can't remember where exactly I read it but: when swapon is called, a fixed-size(determined at swapon) bitmap of the swap blocks is generated (to cope with fragementation of swapfiles).
Can somebody confirm this?

Jan Engelhardt