Andy's Unix FAQ

Please explain the term Virtual Memory and the concept of Paging (Solaris & Linux)

In this FAQ: Virtual memory, Scheduler, Commands to check memory usage,
Why Unix uses all available memory.



Unix FAQ Menu
Contents
Basic commands
Cron
Creating CDs
Device Files
DHCP server (Solaris)
Filesystem explained
Fsck
grub/lilo vanished!
Linux applications?
Linux databases?
Linux distributions
Serial Console
Solaris devices
Solaris disks - Intro
Solaris disks - Adding
Solaris x86 install
SQL/Shell script
Syslog/Monitoring
Time Synchronisation.
Virtual Memory
Web Multi-Language
Web Server Errors
Humour
Unix a Prank



 

Excellent This page has received an average rating of 90% from 20 readers

Question:

Could you please describe for me how the:

Works for the operating systems Linux
For example, could you tell me how the memory manager handles memory using the memory manager. Does it use Demand Paging? Segmented Paging? If it does use paging, does it handle pages on a first come first served basis?

Thank you,

Answer;

The Linux virtual memory handler, in common with most modern Unix variants uses demand paging in most situations. In very extreme memory shortfall situations the handler may revert to process swapping, this is where an entire process is swapped out of main memory.
Swapping is very rarely witnessed at all on most systems.

Segmented paging - where segments of a program are replaced under the control of that same program is not used in Unix.

How memory is managed under Unix

When talking about Unix, 'memory' generally refers to virtual memory ("VM") and not simple physical memory (RAM/Random Access Memory). An explanation of a few terms is in order;

Virtual Memory The entire memory space available to the Unix Kernel. This consists of both the physical memory and all swap space.

Physical memory
a.k.a "RAM"

Physical memory is the RAM attached to the motherboard. This is the stuff you go out and buy and fit to your computer. It does NOT include CPU cache, video memory or other memory attached to controller cards etc..
Swap space
a.k.a Paging space,
Paging area
The space on disk used to extend the physical memory of a system. Swap space is a how a system with 64MB Ram can run a 100MB program.
With rare exceptions all Unix systems should have at least one Swap space
Paging The action of transferring one Page of physical memory from, or to, the swap space. A page is typically in the order of 4KB.
Swapping The action of transferring an entire program (code & data) between Physical memory and the Swap space.
Note that very few Unix systems ever swap in practice.

One cannot describe the virtual memory system as a working on a first-come, first-served basis. Just after boot when most physical RAM is free then this statement is generally true, however in normal operation the algorithm is a considerably more sophisticated.

The virtual memory handler attempts to maintain a pool of free RAM at all times. The precise algorithm varies between variants and versions, but in broad terms the handler uses three variables to control it's activity;

Variable Notes
lotsfree When the free memory pool is above lotsfree the VM handler is idle. If it drops below the handler will begin scanning for unused pages to place on the Free List
desfree 'Desirable Free'. Below this point the handler will start work in earnest applying various criteria to reclaim inuse pages that (say) have not be accessed for some time. The effort and priority of this work increases as the free memory pool drops towards....
minfree Things are getting desperate if the Free memory pool reaches this point. The system may begin thrashing and if the situation is not cleared desperation swapping.
These variables are generally set automatically by the kernel at boot time and are usually based on the amount of physical RAM available to it. It is possible to tune these values manually but this is a highly skilled procedure and beyond the scope of this document. For Solaris see Paging-Related Tunables

At system startup the free memory pool will steadily fall as new programs are started as part of the boot process. What happens next will vary with the functions being performed by the system.

The algorithm used by the VM handler when the free memory pool drops below desfree is complex, varies enormously by platform and the reader should refer to their system documentation for specifics. Generally when the VM handler is called on by the kernel to free memory, each page of memory is analysed using several criteria, including usage count and whether the page contains code or data, and in the later case whether it private to one process or shared amongst several.

Virtual Memory in the Real World

Servers (e.g. fileservers, database servers, web servers) have a generally static process load. As such their free memory pool should normally remain above desfree. If a server consistently shows high VM handler activity during normal operation then it is short of physical memory.

Workstations by their nature are used for many different purposes and gauging their RAM requirements can be difficult. Starting a new application will likely push the free memory pool below desfree, and this may be a frequent occurrence. The only generalisation one can make is that if the VM handler is working hard during normal workload then it may be short of RAM. Of course it could be that the user is making unreasonable demands of the the machine. Remedying office political problems is beyond the scope of this document

The commands "vmstat" and "sar" can be used to observe virtual memory activity. Typically you would run vmstat with an interval - i.e. "vmstat 5".

Bear in mind that Unix uses physical memory for many things other than just programs and data. For example many filesystem and network related structures are cached for performance.

Unix uses all physical memory all the time
On a smoothly running Unix system all physical memory (less the free pool) will appear to be in use all the time. This is quite normal and should not be a cause for concern - it simply represents the most efficient use of a valuable resource. If you are concerned that your system is short of memory you should be examining the paging scan and page-out rates of your system - 'sr' & 'po' columns in vmstat.

The Scheduler

I don't fully understand what you need by the term "Processor Manager", I suspect you probably mean the process scheduler.

The scheduler is responsible for allocating CPU time to those processes on the runqueue. The runqueue is simply a list of processes that currently require CPU time, as opposed to those waiting for some external event, such as a data I/O operation to complete, a user prompt, etc.. Normally the scheduler evenly allocates CPU time amongst those processes that require it, however issues like priority, and the real-time status of a process are also factored into this calculation.

Commands to observe scheduler activity are "vmstat", "sar", "top", and "ps". Process priority can be modified using the the 'nice' and 'renice' commands.

Commands to Check memory.

Two commands to check memory usage on a system wide basis are 'vmstat' and 'sar'. My favourite being vmstat. The vmstat command below shows the amount of swap and physical memory free - in Kb. The columns 're','mf','pi','po',and 'fr' indicate the level of paging activity. Note the first line shows averages since the system booted, subsequent lines the activity over the last 5 seconds.

sun (ksh) % vmstat 5
procs    memory        page       disk              faults       cpu
r b w swap   free re mf pi po fr de sr s1 s3 s6 -- in  sy  cs us sy id
0 0 0 134848 41988 0  4 20  4  6  0  0  9  2  0  0 54 145  58  5  4  92
0 0 0 132448 29632 0  2  0  0  0  0  0  0  0  0  0 22  56  41  0  1  99
0 0 0 132448 29632 0  0  0  0  0  0  0  0  0  0  0 11  48  31  0  0 100
0 0 0 132440 29624 0  0  0  0  0  0  0  0  0  0  0 22  56  43  0  1  99
0 0 0 132440 29624 0  0  0  0  0  0  0  1  6  0  0 54  47  34  0  1  99
<Ctrl-C>

sun (ksh) %

Process memory usage can be examined using the '-l' option of ps - 'SZ' column;

sun (ksh) % ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
8 S 10004 3625 3623 0 51 20 f626f338 430 f626f3a4 pts/1 0:01 ksh
sun (ksh) %

Here our shell is occupying 430 pages of virtual memory. On this system 1 page = 4096 bytes, so 430 pages is about 1.7MB. Note - page size varies between platforms

Footnotes;

thrashing is where the system is spending considerably more time and effort performing memory management functions than it is running applications. The term is overused, and frequently misused.

desperation swapping is where the virtual memory handler cannot identify pages to shift to the swap space and instead resorts, as a desperate last measure, to swapping an entire program out. Very rarely witnessed in normal operation.

 

Reference;
IMHO the best printed work covering the internals of most Unix operating system is;
"Design of the Unix operating system", by Maurice Bach; http://hallcomputer.com/operating_systems/95.shtml

Good complete works for Linux are thin on the ground, such is the nature of open source development. One of the best works is a thesis by Mel Gorman, available from the University of Limerick;
http://www.csn.ul.ie/~mel/projects/vm/


Feedback

I hope you found this FAQ to be of some use. It would be most helpful if you could rate it below. All fields are optional...
Please do not use this form to seek free technical assistance - Try AllExperts...

Excellent Your Email:
Good Comments or Suggestions
Useful
Slightly useful
Not useful
        

 



Home Thai Guide   Great Circle Calculator WorldClock AMS Services Contact us