LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Why swap used? (https://www.linuxquestions.org/questions/linux-newbie-8/why-swap-used-771623/)

your_shadow03 11-25-2009 11:08 PM

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

Web31337 11-25-2009 11:35 PM

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


ronlau9 11-26-2009 03:09 AM

If you never hibernate and have enough RAM you do not swap file

shpenat 11-26-2009 04:40 AM

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.

pcunix 11-26-2009 06:10 AM

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?

johnsfine 11-26-2009 08:27 AM

Quote:

Originally Posted by pcunix (Post 3770249)
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.

pcunix 11-26-2009 08:57 AM

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

i92guboj 11-26-2009 02:13 PM

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


All times are GMT -5. The time now is 11:54 AM.