Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Other Things Gentoo
  • Search

swap file instead of swap partition

Still need help with Gentoo, and your question doesn't fit in the above forums? Here is your last bastion of hope.
Post Reply
Advanced search
13 posts • Page 1 of 1
Author
Message
momesana
Apprentice
Apprentice
User avatar
Posts: 212
Joined: Mon Sep 20, 2004 3:06 pm
Location: Germany (Bremen)

swap file instead of swap partition

  • Quote

Post by momesana » Sun Sep 11, 2005 9:28 pm

I have about 768 MB of Ram and initially I thought that I would not need a swap partition any longer but still I run out of memory occasionaly when I am emerging something, browsing the web with firefox, listening to music with amarok and doing other things, simultanously. It's wierd. The system becomes locked for hours before it recovers from it. Everytime this happens, I see that I have run out of memory and my processor is under extreme load. A swap partition would probably remedy this but I do not want to repartition my 120 GB harddisk, that now has only a few GB of unused space. Can I just create a file and use it as ram? Someone told me that this may be possible in conjunction with the loopdevice. then I would create a file, mount it on the loopdevice and use it as ram or something like that. I don't have the vaguest notion of how to achieve this and I am not even sure if it is possible at all, so I am asking you ...


thx in advance
in the world I see, you're stalking elk throught the grand canyon forests, around the ruins of the rockefeller center. You'll wear leather clothes, that will last you the rest of your life... (Tylers Vision)
Top
platojones
Veteran
Veteran
User avatar
Posts: 1602
Joined: Wed Oct 23, 2002 10:48 pm
Location: Just over the horizon

  • Quote

Post by platojones » Sun Sep 11, 2005 9:52 pm

[UPDATE] This looked too easy to be true, so I just tried it. Takes about 1 minute to do it. Works perfectly.

If you look at the mkswap manpage it has some useful information about how to create a swap file. Based on what that says, I feel confident that it is possible (maybe even simple) to use a file for swap. The steps implied by this are:

1) Create a swap file using (from the bottom of the man page). Of course, you may want to make it bigger.
To setup a swap file, it is necessary to create that file before
initializing it with mkswap , e.g. using a command like

# dd if=/dev/zero of=swapfile bs=1024 count=65536

Note that a swap file must not contain any holes (so, using cp(1)
to create the file is not acceptable).
2) Initialize the swap file with the mkswap (using the file path instead of a device name)

3) Put the swapfile entry in /etc/fstab, referencing the full file path instead of the device. Make sure that the partition you put the swap file on is mounted *before* init tries to turn on swap. I think it should go by the order in fstab. You don't need a loopback device at all. You should be able to test the file before commiting it to fstab with the swapon 'swapfilepath' command. I've never done this, so let me know how it works out :D
Top
momesana
Apprentice
Apprentice
User avatar
Posts: 212
Joined: Mon Sep 20, 2004 3:06 pm
Location: Germany (Bremen)

  • Quote

Post by momesana » Mon Sep 12, 2005 6:52 am

platojones wrote:[UPDATE]You should be able to test the file before commiting it to fstab with the swapon 'swapfilepath' command. I've never done this, so let me know how it works out :D
Ok, I issued the commands and created a 512 MB swapfile in /mnt. It seems to work but let me test it for a while :)

Thanx alot :)

p.s
localhost mnt # free -m
total used free shared buffers cached
Mem: 757 741 15 0 153 187
-/+ buffers/cache: 400 356
Swap: 511 0 511
in the world I see, you're stalking elk throught the grand canyon forests, around the ruins of the rockefeller center. You'll wear leather clothes, that will last you the rest of your life... (Tylers Vision)
Top
chetan13
Tux's lil' helper
Tux's lil' helper
Posts: 125
Joined: Wed Jul 02, 2003 4:00 pm
Location: chennai,india

  • Quote

Post by chetan13 » Wed Oct 05, 2005 8:55 pm

so did your problem with the system getting locked for hours go away? I'm facing the same problem and if using the swap file worked for you, i'll also do the same.
Top
momesana
Apprentice
Apprentice
User avatar
Posts: 212
Joined: Mon Sep 20, 2004 3:06 pm
Location: Germany (Bremen)

  • Quote

Post by momesana » Wed Oct 05, 2005 10:39 pm

chetan13 wrote:so did your problem with the system getting locked for hours go away? I'm facing the same problem and if using the swap file worked for you, i'll also do the same.
yeah, it did work. But the problem is that initializing a swapfile takes an eternity in the first place. So I stopped mouting the swapfile at boot, and rather do it whenever I need to after having booted the system. This way, it can be done in the background while I am already working on the system. I guess buying another stick of ram is the best solution if one wants to spend the money. Having a Gigabyte of permanent swap-partition is the next best thing to have. The last alternative to resort to is to use a swapfile. But for me this is the only viable alternative, since I am not going to change my partition-scheme and I am not yet willing to pay another 100 Euro for a ram-stick.
And the lockups are indeed not happening anymore.

Good luck :)
in the world I see, you're stalking elk throught the grand canyon forests, around the ruins of the rockefeller center. You'll wear leather clothes, that will last you the rest of your life... (Tylers Vision)
Top
platojones
Veteran
Veteran
User avatar
Posts: 1602
Joined: Wed Oct 23, 2002 10:48 pm
Location: Just over the horizon

  • Quote

Post by platojones » Thu Oct 06, 2005 12:06 am

But the problem is that initializing a swapfile takes an eternity in the first place. So I stopped mouting the swapfile at boot, and rather do it whenever I need to after having booted the system. This way, it can be done in the background while I am already working on the system.
Do you mean that mounting the swap file takes a long time? You only have to initialize it once with mkswap. From that point on, all you have to do is 'mount' it.
If it does take a long time to mount, there may be a problem with the setup. There may be some tuning that can be done to alleviate that.
Top
momesana
Apprentice
Apprentice
User avatar
Posts: 212
Joined: Mon Sep 20, 2004 3:06 pm
Location: Germany (Bremen)

  • Quote

Post by momesana » Thu Oct 06, 2005 5:48 pm

platojones wrote: Do you mean that mounting the swap file takes a long time? You only have to initialize it once with mkswap. From that point on, all you have to do is 'mount' it.
If it does take a long time to mount, there may be a problem with the setup. There may be some tuning that can be done to alleviate that.
It takes almost one and a half minutes.
I don't think, that my fstab entries were wrong:

Code: Select all

/mnt/swapfile          none            swap            sw              0 0
See for yourself:

Code: Select all

 date && swapon /mnt/swapfile && date
Do Okt  6 19:21:23 CEST 2005
Do Okt  6 19:22:50 CEST 2005
1,5 Minutes are just too long during the boot-process. They are however not that annoying when you are already logged in and working on the system, while swapon is being executed in the background.
:)
in the world I see, you're stalking elk throught the grand canyon forests, around the ruins of the rockefeller center. You'll wear leather clothes, that will last you the rest of your life... (Tylers Vision)
Top
platojones
Veteran
Veteran
User avatar
Posts: 1602
Joined: Wed Oct 23, 2002 10:48 pm
Location: Just over the horizon

  • Quote

Post by platojones » Thu Oct 06, 2005 11:36 pm

It takes almost one and a half minutes.
That is surprising. I can't imagine what swapon is doing that takes 1.5 minutes, though I'm not questioning your results. The test I did was with a very small swap partition, so it was obviously not respresentative of a 512 Mb swap file. I have a 2 Gb swap partition on my box and it probably takes less than a second to mount it, so I've never even bothered to time it. I'm assuming your swap entry in fstab comes after you've mounted the partition which it resides on. That would be the only thing I can think of that might cause problems, though I would expect that to result in a deadlock, not a long delay. Ah well, it works, in a fashion, and thus saves some money. I would advise that you set aside a swap partition in the future, should you re-install, no matter how much memory you have. In my years working with Unix-like systems, even if you have more RAM than you think you will ever need, the system performs much better with it. Unix is tightly bound to the VM concept and it suffers without it.
Top
MrUlterior
Guru
Guru
Posts: 511
Joined: Tue Mar 22, 2005 4:25 pm
Location: Switzerland
Contact:
Contact MrUlterior
Website

  • Quote

Post by MrUlterior » Fri Oct 07, 2005 1:30 pm

momesana wrote:

Code: Select all

 date && swapon /mnt/swapfile && date
Do Okt  6 19:21:23 CEST 2005
Do Okt  6 19:22:50 CEST 2005
8O
Hehehe, you do realise there's "time"? :)
eg.

Code: Select all

time swapon /mnt/swapfile
But that's an interesting use of "date" :)

Misanthropy 2.0 - enough hate to go around
Top
iTux
Guru
Guru
Posts: 586
Joined: Tue Sep 07, 2004 11:50 pm
Location: Toronto

  • Quote

Post by iTux » Sat Oct 08, 2005 6:36 pm

Hi,

You could always have shrink one of your partition and create a new one with the available space after that partition.

I don't know much about difference in performance of partitition vs file but the swap partition is probably more efficient as it is consecutive on disk and at a fix location.

Even if you have 768MB or more, you should always have some swap space. Why? Infrequently accessed pages of memory will go on disk, freeing up your memory for other things such as disk buffer and disk cache.

I'm not too sure why your system will locked out for hours.

When no more memory is available, the OOM killer is invoked and it starts killing processes to get memory. It should not lock or bring the system down. Some of your applications (includes the possibility of X - graphical system) might get terminated though. But should not locked out for hours or bring it down. So there is some issue with your system.

But start by adding swap space, you should had created some in the first place when you installed your system - unless you are really limited in disk space...


iTux
Top
momesana
Apprentice
Apprentice
User avatar
Posts: 212
Joined: Mon Sep 20, 2004 3:06 pm
Location: Germany (Bremen)

  • Quote

Post by momesana » Fri Oct 14, 2005 10:56 am

iTux wrote:Hi,

You could always have shrink one of your partition and create a new one with the available space after that partition.
I have reiserfs partitions and with those its easy to enlarge partitions but hard or impossible to shrink them
iTux wrote: When no more memory is available, the OOM killer is invoked and it starts killing processes to get memory. It should not lock or bring the system down. Some of your applications (includes the possibility of X - graphical system) might get terminated though. But should not locked out for hours or bring it down. So there is some issue with your system.
Some other people had the same problem. The system normally recovers after it terminates Firefox etc. and kwin always crashes so I have to restart it with kwin -replace ...
iTux wrote: But start by adding swap space, you should had created some in the first place when you installed your system - unless you are really limited in disk space...
I have now over 300 GB of diskspace since I recently bought a new 200 GB hard. I will repartition the discs as soon as Reiser4 is included in the vanilla kernel (I guess/hope 2.6.16 - 2.6.18), until then the swapfile trick will suffice and it really does work! The system doesn't lock anymore.

Thanx for your suggestions :)
in the world I see, you're stalking elk throught the grand canyon forests, around the ruins of the rockefeller center. You'll wear leather clothes, that will last you the rest of your life... (Tylers Vision)
Top
nephros
Advocate
Advocate
User avatar
Posts: 2139
Joined: Fri Feb 07, 2003 2:46 am
Location: Graz, Austria (Europe - no kangaroos.)
Contact:
Contact nephros
Website

  • Quote

Post by nephros » Sat Oct 15, 2005 12:31 pm

It takes almost one and a half minutes.
I just tried it on reiserfs:

Code: Select all

[scourge]swp # time dd if=/dev/zero of=swap.img bs=1k count=524288
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 8.97858 seconds, 59.8 MB/s

real    0m8.980s
user    0m0.228s
sys     0m8.489s
[scourge]swp # time mkswap swap.img 
Setting up swapspace version 1, size = 536866 kB
no label, UUID=133c485e-223f-4b6f-8cf8-9cd856726143

real    0m6.004s
user    0m0.000s
sys     0m0.152s
[scourge]swp # time swapon swap.img 

real    0m2.638s
user    0m0.000s
sys     0m2.632s
on XFS:

Code: Select all

[scourge]home # time dd if=/dev/zero of=swap.img bs=1k count=524288
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 9.97572 seconds, 53.8 MB/s

real    0m10.006s
user    0m0.160s
sys     0m3.980s
[scourge]home # time mkswap swap.img 
Setting up swapspace version 1, size = 536866 kB
no label, UUID=f9f984d1-a600-46c5-bfeb-5c75ebd043cc

real    0m1.444s
user    0m0.000s
sys     0m0.068s
[scourge]home # time swapon swap.img 

real    0m0.419s
user    0m0.000s
sys     0m0.420s
on ext3:

Code: Select all

[scourge]tmp # time dd if=/dev/zero of=swap.img bs=1k count=524288
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 7.50266 seconds, 71.6 MB/s

real    0m7.516s
user    0m0.200s
sys     0m6.760s
[scourge]tmp # time mkswap swap.img 
Setting up swapspace version 1, size = 536866 kB
no label, UUID=cbb5deda-5d03-4caf-b6dc-61e80170cd4b

real    0m0.440s
user    0m0.000s
sys     0m0.356s
[scourge]tmp # time swapon swap.img 

real    0m1.024s
user    0m0.000s
sys     0m1.012s
and just for fun on FAT32 (slower disk though):

Code: Select all

[scourge]winshare # time dd if=/dev/zero of=swap.img bs=1k count=524288
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 25.3896 seconds, 21.1 MB/s

real    0m25.390s
user    0m0.192s
sys     0m5.260s
[scourge]winshare # time mkswap swap.img 
Setting up swapspace version 1, size = 536866 kB
no label, UUID=b334d198-b660-4fb6-a912-93d193ab38d8

real    0m23.959s
user    0m0.000s
sys     0m0.976s
[scourge]winshare # time swapon swap.img 

real    0m0.474s
user    0m0.000s
sys     0m0.444s
It seems reiserfs it not the ideal filesystem for large contiguous files.
Do you have any other filesystems at your disposal you could make the swap partition on?

If not I'd recommend taking the entry out of fstab and putting the line

Code: Select all

swapon /path/to/file &
in /etc/conf.d/local.start.
This way your swap file will be used at every boot but you won't have to wait for swapon to finish.
Please put [SOLVED] in your topic if you are a moron.
Top
momesana
Apprentice
Apprentice
User avatar
Posts: 212
Joined: Mon Sep 20, 2004 3:06 pm
Location: Germany (Bremen)

reiser4

  • Quote

Post by momesana » Sun Dec 11, 2005 5:59 pm

I installed a reiser4-enabled gentoo-source kernel (version 2.6.14-r2) and reformatted my reiser3 / partition with reiser4. Then I deleted the old swapfile and created a new one and to my surprise I noticed, it didn't take more than a second to mount is with swapon.

Code: Select all

localhost mnt # time swapon swapfile

real    0m0.546s
user    0m0.000s
sys     0m0.468s
localhost mnt # free -m
             total       used       free     shared    buffers     cached
Mem:           756        718         38          0         10        256
-/+ buffers/cache:        451        304
Swap:          511          0        511
localhost mnt #   
funny, isn't it?
in the world I see, you're stalking elk throught the grand canyon forests, around the ruins of the rockefeller center. You'll wear leather clothes, that will last you the rest of your life... (Tylers Vision)
Top
Post Reply

13 posts • Page 1 of 1

Return to “Other Things Gentoo”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic