LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2009, 11:08 PM   #1
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,462
Blog Entries: 6

Rep: Reputation: 51
Why swap used?


I read about swap:
"It is possible to run a Linux system without a swap space, and the system will run well if you have a large amount of memory -- but if you run out of physical memory then the system will crash, as it has nothing else it can do, so it is advisable to have a swap space, especially since disk space is relatively cheap."

My free -m says:

Code:
 free -m
             total       used       free     shared    buffers     cached
Mem:          7866       4014       3852          0         35       1299
-/+ buffers/cache:       2679       5187
Swap:         8189       1042       7146
[root@ComHemDev2 ~]#
Physical memory is enough. Then why its using 1042 swap?
Confused !!
 
Old 11-25-2009, 11:35 PM   #2
Web31337
Member
 
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Blog Entries: 71

Rep: Reputation: 65
i run my debian without swap.
Code:
root@srvr:~# free -m
             total       used       free     shared    buffers     cached
Mem:          2027       1974         53          0          9        809
-/+ buffers/cache:       1155        872
Swap:            0          0          0
but need more RAM since i run several servers here and also a virtualbox.
never really crashed.
i disabled swap on my fstab:
Code:
root@srvr:~# cat /etc/fstab
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
UUID=ad4ade0f-7ffe-4df1-b567-7e730dfcb6d1       /               ext3    errors=remount-ro 0       1
UUID=879907d8-a709-4258-b1a3-c2fd29ce7ac6       /home           ext3    defaults        0       2
#/dev/sda2       none            swap    sw              0       0
/dev/hda        /media/cdrom0   udf,iso9660 user,noauto     0       0
UUID=45e3b3b8-938f-4ccf-966a-c70ef70b808b	/home/web31337/300G	reiserfs	defaults 0 3
UUID=6abc5ddd-165d-45a7-8de6-749cdbcdc98f	/home/web31337/500G	ext3		defaults 0 4
 
Old 11-26-2009, 03:09 AM   #3
ronlau9
Senior Member
 
Registered: Dec 2007
Location: In front of my LINUX OR MAC BOX
Distribution: Mandriva 2009 X86_64 suse 11.3 X86_64 Centos X86_64 Debian X86_64 Linux MInt 86_64 OS X
Posts: 2,369

Rep: Reputation: Disabled
If you never hibernate and have enough RAM you do not swap file
 
Old 11-26-2009, 04:40 AM   #4
shpenat
Member
 
Registered: Dec 2008
Distribution: LFS
Posts: 95

Rep: Reputation: 21
Linux kernel uses swap (if available) even when having enough memory. When you have idle application just sitting in RAM doing nothing it is good thing to move it to swap so it does not occupy usable RAM.

Look for "swappiness" kernel parameter for more details.
 
Old 11-26-2009, 06:10 AM   #5
pcunix
Member
 
Registered: Dec 2004
Location: MA
Distribution: Various
Posts: 149

Rep: Reputation: 23
I'm not up on the latest kernel designs, but the historical reason that you needed swap even if you had full memory was that systems would swap out unused processes even if memory wasn't needed. One justification for that would be to make more room for disk caching, another might be to make room for memory that active processes might soon request.

Perhaps someone who is up on current design can tell us where it is now?
 
Old 11-26-2009, 08:27 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by pcunix View Post
the historical reason that you needed swap even if you had full memory was that systems would swap out unused processes even if memory wasn't needed.
That isn't just historical. That is current design and it is a good design. But the word "needed" is misleading. I don't think Linux ever needed swap when there is enough ram. It may make good use of swap if available even if there is enough ram.

Quote:
One justification for that would be to make more room for disk caching,
That is the usual reason.

Quote:
another might be to make room for memory that active processes might soon request.
I think inactive dirty pages of anonymous memory can be written to the swap file without being discarded from the page cache. So if the process owning those pages suddenly starts using them again, they are still in ram. But if some other process uses a lot of memory (or file I/O using a lot of file caching) the ram is available immediately by dropping those inactive pages from the page cache. So an unnecessary write to the swap file when there wasn't a system load might do nothing or it might save the time needed to do the write later when the load is higher.
 
Old 11-26-2009, 08:57 AM   #7
pcunix
Member
 
Registered: Dec 2004
Location: MA
Distribution: Various
Posts: 149

Rep: Reputation: 23
Yes, I should have said "wanted" or "might want".

There's also the good advice that the kernel probably knows better than you do when it comes to managing memory.

On the other hand: you don't want tons of swap. For example, now that memory is no longer limited to 4 GB, you could theoretically have many gigabytes of swap available for paging, but paging a very large process is not necessarily what you want. I may not be using BigApp for a while, but when I do switch to it, I don't want to wait for it to be paged in.

But - then we go back to my switching to OtherBigApp and having to wait because what I asked it to do needs memory that BigApp has tied up.

Until we have so much fast ram that we never need all of it, there will always be trade-offs. I expect we'll see that day eventually - we already see it with hard drives (at least for most of us). I imagine that a future post here might begin "Back in the days when nobody had enough RAM to run all their apps..."

Last edited by pcunix; 11-26-2009 at 08:58 AM. Reason: spelling
 
Old 11-26-2009, 02:13 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,073

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
The right question is not that, the right question is "why not to use swap when it increases performance even if it's by a small edge?" Is really that little file (you don't even need a partition) really bothering you that much?

It's a recurrent topic so I bookmarked one explanation some time ago,

http://www.linuxforums.org/forum/lin...tml#post686033

Quote:
"...but if you run out of physical memory then the system will crash, as it has nothing else it can do,...
It shouldn't crash "the system" though. At most, your kernel OOM killer will start closing applications, probably making you lose all your work that you haven't saved in one hour.

When you have swap and it starts filling up, the performance degradation is like an alarm that tells you "save your work NOW!".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to swap out (to swap area) page frames a process use? entertainyou Linux - Kernel 3 03-23-2011 09:52 PM
Can swap file or a swap partition be mounted and read? Mr-Bisquit Linux - General 3 06-08-2009 02:16 PM
SWAP - having trouble with swap auto mounting at boot lostdj Linux - General 2 08-05-2008 12:47 PM
How to unmount actual swap and mount a new(bigger) swap space? isaac Linux - Newbie 1 06-06-2004 01:23 AM
Difference between Swap Virtrual memory and Swap Parition Nappa Slackware 4 11-27-2003 07:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration