Please advise good source of info about swapping

Hi Everyone,

I have some misunderstanding about swapping.

Is it true that swap
a) required always (I mean I cannot set size 0 bytes)
and
b) some pages are saved on disk in idle time even free memory is enough
?

Could you please advise some good reading about it?

Whether it's required or not depends on the operating system.

Whether pages are saved to disk in idle times also depends on the operating system but these days, often not. An operating system will often pre-claim space inside the swap though, in case it needs to do so later.

Whether or not it's required, having swap is a good idea. Without it, you may run out of memory and deadlock or die.

Corona688, thank you for your comment.

Could you please be more specific about operating systems?
I mean Solaris, Linux. Is swap must be on these platforms?

Why without swap I may run out of memory but with swap not?
If I have 8Gb RAM and 8Gb swap and an application requests 20Gb will it run out of memory?

And I will try be more specific about the question.
Let's suggest that I have some soft, for example, Oracle database.
And I am worry that with swap my OS will save pages on the disk in background even it is not required.
Am I right?

[duplicate]

These systems respond somewhat differently to swap.

If you ask for 20GB of RAM in Linux it will reserve you that much address space, not that much RAM. Only when you actually start using these individual pages of RAM will it give you actual memory. If programs end up using more RAM than you actually have, pieces will start having to end up in swap. Without swap, or when the swap's full, the kernel will start killing the worst offenders to reclaim some RAM and stop the system from deadlocking. This is known as 'overcommit' and can be disabled if you really want, but is useful for some situations; the google search engine's servers apparently rely heavily on the Linux OOM-killer.

Some other systems(HP-UX if I recall) don't have any sort of overcommit. All memory must be backed by swap of some sort. If you don't have enough, some memory will be used to act like swap for other memory.

I think Solaris is somewhere between these two extremes.

There is a substantial Wikipedia on this subject (and links to more articles):
Paging - Wikipedia, the free encyclopedia

No: The request will be refused.
Btw. The old rule that you must have at least as much swap as you have memory does not apply to modern unix.

Many modern unix systems can be configured to use part of memory for swap. This allows the O/S to defragment main memory quickly and quietly.

If you have Oracle, buy enough memory so that it should never swap and be sure that you understand how to size the SGA and PGA and how to measure these sizes using Oracle itself. It is very common to missize the SGA by a factor of 8 or even 16.

Guys,
Thanks for your comments.

If you have Oracle, buy enough memory so that it should never swap

1) Does this mean that OS will not do unnecessary paging in background?

2) Does this mean that if I have 128Gb of RAM and if I am sure that my soft (for example Oracle) will not use more than 120Gb of RAM (SGA + PGA = 110Gb), I give 8 Gb for OS (kernel, etc) and I can set swap size 0 bytes ?

3) For 8Gb RAM and 8Gb swap how much memory I can eat (really)? As I understand I can 16Gb. Am I right?
Why there was a requirement to have swap the same size as RAM? Why not 3 size of RAM or 4 sizes of RAM?
The requirement to have swap the same size of RAM looks like swap not add space to memory but something like mirror RAM memory on a disk. In this case I would be able to eat 8Gb, not 16Gb.

Please comment

---------- Post updated at 11:42 AM ---------- Previous update was at 11:36 AM ----------

From your comments I can consider that swap is just something as additional slow memory :wink:
But from my current vision concept of swap is more important and indivisible with virtual memory concept.

It doesn't make swap necessary. Virtual memory makes swap possible because the OS can leave triggers for itself when certain memory is used, i.e. automatically pausing a process and sending a request to the OS when the process attempts to access a swapped-out page. The ability to flag and reorder memory as you please makes other things possible too, like read-only memory, shared memory, and mapped files...

Whether swap is necessary and how much depends on the UNIX. The behavior when the system runs out of memory also depends on the UNIX.

I don't agree that no swap is advisable. Swap is your safety buffer. And depending on how you configure it, may not be that slow either. If you put it on the same spindle as your applications it'll naturally be slow...

1) The O/S will do necessary paging as processes are created and die. If you have no swap, memory will become fragmented.

2) You should never set the swap size to zero because the O/S still needs to shuffle pages around in the background. While you are tuning Oracle you need substantial leeway and I would have swap at least the same size as memory to allow for future expansion, unexpected peaks and configuration mistakes. I'd rather have the system run slow than crash.

Oracle makes extensive use of O/S Shared Memory and you must allow memory for this and build the kernel correctly.

Most unix O/S dedicate 25% of memory to disc buffers by default. You either allow for it or change the kernel parameters. If the system only ever runs Oracle then there is much scope to give memory to Oracle disc buffers.
Many Oracle DBAs don't allocate enough memory for table sorts in the SGA.

There are huge and comprehensive manuals from Oracle regarding sizing and tuning on each different O/S. In my experience Oracle will never run properly straight out of the box.

3) Yes if you have 8Gb RAM and 8Gb swap the most virtual memory you could ever use is 16Gb.
There was a historic requirement to have a minimum swap of the same size as memory and older unix would not let you use any physical memory above that point. There was no mirroring, but swap was also used for dump in the event of a system crash and it had to be big enough. That rule has been relaxed on some modern O/S.
Older unix did indeed recommend creating swap at 2x or even 3x memory and swap was used for storing idle processes on heavily loaded systems (but it was slow if any active processes got displaced into swap).

Guys, thanks for your valuable comments.

Could you please also comment:
1) If soft does NOT feet all available RAM - should swap be free or not?
As I understood - not, because it should solve memory defragmentation?
Is it right?

2) If you see that your host has 64Gb of RAM, has 32 Gb of allocated swap (full available swap is 64Gb for example) - is it problem?

3) vmstat shows pi po - does it indicate a problem?

4) How to determine that host is having problem with the swap?

---------- Post updated at 04:28 AM ---------- Previous update was at 04:16 AM ----------

May I ask about the windows. I know that this forum is about unix, but the question is related with the conversation about swap concept.

I am seing in task manager physical memory 2Gb, available 200K. Page file usage 2,35Gb and this is the line not changed in the time on the chart in task manager.
Why swap usage is 2,35Gb. Is it allocated_but_not_used size?

---------- Post updated at 04:55 AM ---------- Previous update was at 04:28 AM ----------

an example (vmstat Solaris 10):

 kthr      memory                       page               disk          faults      cpu
 r b w   swap      free      re   mf  pi po fr de sr m0 m1 m3 m4   in   sy      cs   us sy id
 0 5 0 124949008 30882168   1830 11959 0 2  2 0  0  0  0  0  0   51824 522265 65491 31 12 56
 0 3 0 124837640 30861096   1755 8975  0 1  1 0  0  0  0  0  0   47055 499749 56455 33 14 53
 0 3 0 124908888 30861088   1770 10615 6 1  1 0  0  0  0  0  0   47026 487126 57109 37 15 48
 0 2 0 124908920 30847944   1698 9732  0 3  2 0  0  0  0  0  0   45622 497099 56097 34 11 55
 0 2 0 124942672 30873432   1909 11107 0 1  1 0  0  0  0  0  0   43191 504910 50763 34 12 55
 0 2 0 124918896 30856128   1518 8609  0 1  1 0  0  0  0  0  0   46034 516341 55213 34 11 56
 0 4 0 124871216 30823384   1661 9245  0 0  0 0  0  0  0  0  0   47537 531603 57644 35 12 53
 0 5 0 124867672 30831888   1678 10591 2 2  2 0  0  0  0  0  0   43464 530756 52283 33 13 54

Why swap != 0 ? And why swap is changed in time when free memory 30Gb ?

1) Yes.

2) Not necessarily. It only matters if active tasks are being swapped out. It would be a very serious problem if say your database engine was beings swapped out. If all that is swapped out is idle user sessions, it is pretty harmless.

3) Interpretation of vmstat is system specific. Paging is really only an issue if the system actually runs slowly. Paging figures in vmstat can seem high on a modern fast system with fast discs with no detrimental effect whereas the same figures on an older slower system would indicate a serious problem. You need to monitor your system over an extended period of time to determine what is "normal".
Beware of overmonitoring. I've seen a system brought to its knees by C.A. Unicenter software. Good old unix "sar" is still a useful tool when configured correctly.
In an Oracle environment you can get much useful performance information from the database statistics.

4) Apart from the obvious where the system feels slow or even stops dead for several seconds, there are many other warning signs. I/O queue for discs (especially one containing a swap partition) is one. A severely overloaded unix system will log errors in system logs and start killing off tasks to prevent a crash.

5) Windows is highly version specific. Always mention the version.
Most of them allocate page file space to roughly match the size of the running programs plus any overload. Some of the paging technology came from Xenix (an old unix which Microsoft owned for a while) but unix has advance somewhat since.
The figures you post suggest that the Windows system is short of physical memory by about half a Gigabyte.

6) A vmstat running for a few seconds is totally meaningless. What command did you type?
The system will be using swap space to shuffle pages in memory and to page in/out idle pages.
On the system in question is all the swap on disc, or is some of it in memory?

Unless the computer needs to do some serious number crunching we always go for larger memory and faster discs, but fit multiple slower CPUs to keep the costs down.