Help understanding how swap works

Hi all,

I'm trying to get an understanding of swap. This is what I see:

Memory: 8192M real, 1697M free, 5693M swap in use, 10G swap free

My question(s): If I have available RAM, why am I using swap? Or am I reading this wrong (been known to happen).

Thanks much,

~K

It's not 'used' as in 'the system is desperately out of RAM and turfing things onto disk to make room', but 'used' as in 'we have 5 gigabytes of swap reserved to match currently allocated memory'. Most UNIX systems reserve swap in advance, so that if conditions suddenly do become tight, the system doesn't find itself overextended i.e. there's more RAM been given away than it has swap space to deal with.

3 Likes

I think that you're asking why, when you have RAM free, is there so much "swapped out".

The answer is that swap space is managed by the Operating System and is used as an extension of RAM. Otherwise the system would run out of RAM at some point especially when new processes need RAM space to launch.

However, if the working set (or part of a working set) is kicked out of RAM into swap space to make room for somebody else, the process swapped out will not be swapped back into RAM unless there is a need (ie, the process is ready to run). There may be no need to swap a process back in if, for example, it is waiting for I/O (to disk or network) to complete.

So, for example, a large new process starts which causes several other processes to be swapped out. That large process then finishes releasing RAM but there may be no need at that time to swap any processes back in. Hence, some RAM is free but swap space usage is high(er).

I don't know whether I've explained that clearly enough. Does that help?

1 Like

Awesome. Makes more sense now. Many thanks to you both!

Good.

Ever left your terminal waiting for keyboard input and gone out to lunch?

It's not a practice to do so. Why?

Well that's a typical example of when a process is suspended for a whole hour waiting for I/O (from the terminal keyboard) which makes it an ideal candidate to be kicked out of RAM when RAM space is tight, and with no need to be swapped back in until you come back, sit down, type something and hit return.

Yes it is reserved swap.
The swap space that is really written to swap devices is shown with

swap -l

This looks like top output. I wouldn't recommend using it to investigate memory usage. You'd rather use native tools to figure out how much physical, virtual and swap memory is used on you system.

Please post the output of these commands:

swap -s
swap -l
vmstat 2 2
echo ::memstat | mdb -k 
prstat -Zc -n 1,100 1 1

There is no evidence you use or not swap from top output, but there are certainly situations where it is legitimate for swap to be used while plenty of RAM is free. You can even have applications crashing with an out of memory error while plenty of RAM is available.