When is Swap utilised?

Hi Gurus,

I understand that swap space is utilised by the system to swap pages between RAM and swap(disk space) when there is a Memory crunch.

My question is whether the system will wait till the Memory free becomes zero to swap or will it just swap if the page remains idle.

 
[hpinadmin@MUMLHPINDBS001V ~]$ free -m -t
             total       used       free     shared    buffers     cached
Mem:       3821       3707      114          0        456       2906
-/+ buffers/cache:     344      477
Swap:       8191          0       8191
Total:       12013       3707    8306

Appended above is the output of "free" command in one of my linux servers.
The swap is not at all being utilised while as the free memory is as low as "114mb".

Does swap need to be activated?
Is the above output normal for a production server? or should i start looking to scale it up?

Thanks
HG

Why do you want your swap area to be used while you still have free memory ? Also, most of the buffers and cached memory is technically free, i.e. actually available to processes should they need it.

Hi jlliagre,

How are you saying that the buffers and cache are free?

What exactly does the shared/cached output mean in below output:

[hpinadmin@MUMLHPINDBS001V ~]$ free -m -t
             total       used       free     shared    buffers     cached
Mem:       3821       3707      114          0        456       2906
-/+ buffers/cache:     344      477
Swap:       8191          0       8191
Total:       12013       3707    8306

Also i was asked to get the size of each process running in this server.
I captured the RSS of each process in ps -efly output and added them(code appended below).

I ran the following command :

 
[hpinadmin@MUMLHPINAPP001V ~]$ ps -efly | awk '{print $8}' | awk '{sum = sum + $1;} END{print sum}'
670780

This adds to 670mb only. So where is the remaining Memory? If it's cached/shared,where can i find them.

Note - Please bear with my ignorance if i am asking something fundamental.

TIA
HG

Mr Ganesh,

Within UNIX/Linux, the proverb is : "Free memory is wasted memory".

This means that all available memory goes to use within the system to serve as disk-cache, buffers, networking backing memory, etc. etc.
That is why in a UNIX/Linux system that is running for a while, the free memory counter is next to zero, and the cache and buffers counters are way up.

This is expected and approved behavior.

Swapping (or paging as is a better name for it) is not preferred behavior, since swapping pages in and out of memory is a very 'expensive' exercise in terms of time. It causes the applications to stop running, full contexts of the CPU to be flushed, active memory pages to be swapped to disk, other pages from disk to be placed in memory etc etc. this all takes a huge amount of time (CPU clocks wise..)

Before UNIX/Linux will start to swap pages in and out of memory to swapspace, the disk-cache will first 'shrink' to nearly 0, so from your 'free' output, you can count the 2.9 GB of 'cached' as free memory as well.

As for actively used memory, the RSS memory are the 'resident size' pages actively used by the application image. This is not equal to the full size the application has allocated though. Also, there is a SHR memory area, which is the Shared memory pages between the applications, this includes loaded shared libraries that multiple apps can call.

Counting the SHR + RSS and adding them up still will not get you the correctly used memory by the running apps.

However, given this 'free' output, you have nothing to worry about, and swap is correctly not used....

1 Like