Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Swap partition vs swap file

56 views
Skip to first unread message

Mike Richards

unread,
Jun 28, 2005, 9:10:36 PM6/28/05
to linux-...@vger.kernel.org
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?
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Andrew Morton

unread,
Jun 29, 2005, 1:06:30 AM6/29/05
to Mike Richards, linux-...@vger.kernel.org
Mike Richards <mrmik...@gmail.com> wrote:
>
> 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.

Marat Buharov

unread,
Jun 29, 2005, 6:39:28 AM6/29/05
to linux-...@vger.kernel.org
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 <ak...@osdl.org>:

Douglas McNaught

unread,
Jun 29, 2005, 7:59:19 AM6/29/05
to Marat Buharov, linux-...@vger.kernel.org
Marat Buharov <marat....@gmail.com> writes:

> 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

Mike Richards

unread,
Jul 7, 2005, 4:01:12 PM7/7/05
to Andrew Morton, linux-...@vger.kernel.org
> > 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.

Andrew Morton

unread,
Jul 7, 2005, 6:05:52 PM7/7/05
to Mike Richards, linux-...@vger.kernel.org
Mike Richards <mrmik...@gmail.com> wrote:
>
> > > 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.4 is weaker: it has to allocate memory from the main page allocator when
performing swapout. 2.6 avoids that.

> 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.

> 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.

Coywolf Qi Hunt

unread,
Jul 7, 2005, 8:45:41 PM7/7/05
to Andrew Morton, Mike Richards, linux-...@vger.kernel.org
On 7/8/05, Andrew Morton <ak...@osdl.org> wrote:
> Mike Richards <mrmik...@gmail.com> wrote:
> >
> > > > 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.4 is weaker: it has to allocate memory from the main page allocator when
> performing swapout. 2.6 avoids that.
>
> > 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.

> 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.
>

--
Coywolf Qi Hunt
http://ahbl.org/~coywolf/

Bernd Eckenfels

unread,
Jul 7, 2005, 9:26:51 PM7/7/05
to linux-...@vger.kernel.org
In article <2cd57c9005070...@mail.gmail.com> 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

Jeremy Nickurak

unread,
Jul 8, 2005, 11:37:02 AM7/8/05
to linux-...@vger.kernel.org
On ven, 2005-07-08 at 03:22 +0200, Bernd Eckenfels wrote:
> 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.

--
Jeremy Nickurak <at...@lkml.spam.rifetech.com>

signature.asc

Helge Hafting

unread,
Jul 8, 2005, 5:37:29 PM7/8/05
to Jeremy Nickurak, linux-...@vger.kernel.org

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

Wakko Warner

unread,
Jul 8, 2005, 6:48:55 PM7/8/05
to Jeremy Nickurak, linux-...@vger.kernel.org

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)

--
Lab tests show that use of micro$oft causes cancer in lab animals

Eric Sandall

unread,
Jul 9, 2005, 7:00:33 PM7/9/05
to Wakko Warner, Jeremy Nickurak, linux-...@vger.kernel.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, 8 Jul 2005, Wakko Warner wrote:
> Jeremy Nickurak wrote:
>> On ven, 2005-07-08 at 03:22 +0200, Bernd Eckenfels wrote:
>>> 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)

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
er...@sandall.us | 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-----

Wakko Warner

unread,
Jul 9, 2005, 9:29:29 PM7/9/05
to Eric Sandall, Jeremy Nickurak, linux-...@vger.kernel.org
Eric Sandall wrote:
> >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.

--
Lab tests show that use of micro$oft causes cancer in lab animals

Bernd Eckenfels

unread,
Jul 9, 2005, 10:16:55 PM7/9/05
to linux-...@vger.kernel.org
In article <20050710014...@animx.eu.org> 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

Wakko Warner

unread,
Jul 10, 2005, 8:39:36 AM7/10/05
to Bernd Eckenfels, linux-...@vger.kernel.org
Bernd Eckenfels wrote:
> In article <20050710014...@animx.eu.org> 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.

--
Lab tests show that use of micro$oft causes cancer in lab animals

Bernd Eckenfels

unread,
Jul 10, 2005, 5:46:49 PM7/10/05
to linux-...@vger.kernel.org
In article <20050710125...@animx.eu.org> 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.

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

Portability across Filesystems.

Gruss
Bernd

Helge Hafting

unread,
Jul 11, 2005, 7:05:09 AM7/11/05
to Wakko Warner, Bernd Eckenfels, linux-...@vger.kernel.org
Wakko Warner wrote:

>Bernd Eckenfels wrote:
>
>
>>In article <20050710014...@animx.eu.org> 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.
>>
>>

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.

>
>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.

>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

Wakko Warner

unread,
Jul 12, 2005, 5:41:53 PM7/12/05
to Helge Hafting, Bernd Eckenfels, linux-...@vger.kernel.org
Helge Hafting wrote:

> 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.

> >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.

> >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.

> 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.

--
Lab tests show that use of micro$oft causes cancer in lab animals

Jan Engelhardt

unread,
Jul 13, 2005, 7:09:54 AM7/13/05
to Wakko Warner, Helge Hafting, Bernd Eckenfels, linux-...@vger.kernel.org
>> 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
--

0 new messages