OS X Topics

Physical Disk Problems

File System Problems

Bad File System?

HFS+ vs. UFS

Partitioning: an evolving topic

about: swap

 

 

 

 

Site Index

Photography

Technical Topics

 

 

 

 

 

 

 

 

 

 

 

 

 

Member

ACTC / APP

about: swap

Scratch disks vs. SWAP

if you're intimate with the workings of UNIX SWAP space, then this won't be anything new to you...

Scratch disks in legacy Mac OS are a cross between '/tmp' (disk space for temporary files while the app was running) and an application-level hack to let the app control paging what was in memory, and what wasn't, as the system level resources might be nonexistent (VM not enabled) or insufficient (not enough VM to hold the item you were working on).

In contrast UNIX SWAP space is part of a rather complicated memory management hierarchy that lets a running process address/read/write data or program stored in cache, system memory, or file system [and with a hierarchical storage system, past disk out to magnetic tape]. At a high level, each and every process (do 'top' or 'ps aux' or use Process Viewer) and you see a list of running processes) has access to an address space of 4GB (2**32) in OS X. None of us have that much memory, hence this address space is 'virtual' - the physical memory does not exist.

The address space available to the process contains:

'code' pages (typically read-only)

the program that is executing libraries used by the program (from /usr/lib/lib*.dylib)

'data' pages

variables and memory used by the program files read / written by the program

The memory management and paging is split between Mach and BSD - Mach provides the memory management primitives, while the Darwin/BSD 'dynamic pager' process manages the paging and the actual I/O to disk.

The dynamically linked library pages are referred to as Shared Libraries, which is to say, more than one program, or process can access them, so even though you have FTP, OmniWeb, Telnet, and SSH running at the same time, they 'share' the (read-only) code pages for common operations like host name lookups, standard I/O, access to TCP/IP connections & etc.

What memory management does is keep track of which pages are:

private - restricted to a process, or kernel, etc.
code - executable programs or libraries
shared - as in a dynamically linked library or a memory object shared between two processes
wired - some code pages must remain in memory
read-only - cannot be written to (code or file opened read-only)
read-write - page can be written modified or not when the page was last accessed

Basically, each of the processes in the process list is waiting for a signal, I/O, is running or is runable - waiting for some CPU cycles. Paging occurs when there is a requirement to read stuff from disk into memory (loading a program, reading a file, restoring parts of a process which have been swapped out to disk to make it runable) or write stuff in memory out to disk (writing files, or writing modified pages out to disk when a process is swapped out or made non-runable).

Paging occurs when there is not enough real memory available to make a processes runable, or when the process requests some disk I/O. Paging can occur without using SWAP space. All those read-only code pages, and files that have been opened read-only, or have been read and not written, don't need written out to the SWAP area - the mapping of those disk areas to memory pages is preserved within the process, but the memory pages do not need to be written out to disk - they are already there.

SWAP is used when process information or data pages are written to and the process is swapped out, then these 'dirty' (modified) pages are thrown into the SWAP area. Likewise when a swapped process is to become runable, the pages sent to SWAP are restored, as are any required read-only code or data pages from elsewhere on disk. So the space used to swap out a process is thus both smaller than the real memory used while running, and far smaller than the addressable space available.

In some UNIX systems the SWAP area is also used for temporary file systems, such as /tmp (files usually cleared at reboot) and /var/run or /proc (which are used to keep track of currently executing processes and not preserved through reboot). In some systems, after a crash, the system saves the contents of the real memory to the SWAP area on reboot, and this is then saved to disk as a crash dump.

Header lines of 'top' output, describe processes and memory usage:

Processes: 48 total, 2 running, 46 sleeping... 115 threads 15:41:38
Load Avg: 0.00, 0.07, 0.10 CPU usage: 5.4% user, 7.1% sys, 87.5% idle
SharedLibs: num = 87, resident = 16.8M code, 1.38M data, 5.45M LinkEdit
MemRegions: num = 2381, resident = 49.8M + 7.21M private, 46.1M shared
PhysMem: 36.4M wired, 39.4M active, 120M inactive, 195M used, 189M free
36.4M wired + 39.4M active + 120M inactive => 195M used
195M used + 189M free => 384MB (what's installed on the machine)
VM: 894M + 43.1M 9158(0) pageins, 0(0) pageouts

no pageouts only pageins....

Basically, this machine hasn't swapped anything in memory out to the swap file (yet), as there have been no page-outs.

After starting Classic, iTunes, iView MediaPro, Virtual PC Test Drive, that changes -- the system is using most of the physical memory (379 of 384MB) and there have been lots of pageouts. If you run top, you'll probably notice that the system tries to keep a few MB of Physical Memory free.

(And if you're really dangerous you can do stupid UNIX tricks to demonstrate to the casual observer that the swap file went from not being used to being used :-):

Processes: 53 total, 4 running, 49 sleeping... 153 threads 16:33:47
Load Avg: 3.15, 2.98, 2.90 CPU usage: 87.1% user, 12.9% sys, 0.0% idle
SharedLibs: num = 91, resident = 16.4M code, 380K data, 4.16M LinkEdit
MemRegions: num = 3348, resident = 221M + 7.36M private, 66.9M shared
PhysMem: 39.8M wired, 224M active, 116M inactive, 379M used, 4.61M free
VM: 2.30G + 44.1M 10911(0) pageins, 1130(0) pageouts
 
PID COMMAND %CPU TIME #TH #PRTS #MREGS RPRVT RSHRD RSIZE VSIZE
789 iView Medi 2.8% 0:13.77 1 61 101 14.9M 9.49M 20.3M 76.2M
784 iTunes 35.1% 1:36.83 7 147 169 13.7M 8.59M 18.7M 76.5M
757 top 9.3% 1:19.53 1 18 15 276K 220K 496K 1.39M
756 VirtualPC_ 0.0% 0:00.61 1 17 17 104K 204K 380K 1.78M
754 Virtual PC 23.6% 6:09.58 7 107 125 97.9M 12.5M 105M 160M
752 TruBlueEnv 0.0% 0:51.10 15 158 203 41.0M 1.40M 41.9M 1.06G
545 .OmniWeb 0.0% 2:07.62 21 167 402 16.0M 14.3M 23.8M 86.4M
510 AppleSpell 0.0% 0:01.15 1 57 32 724K 2.60M 1.69M 32.4M
496 Mail 0.0% 3:15.37 10 166 162 5.78M 8.93M 11.3M 62.3M
462 DisplaySer 0.0% 0:00.56 2 54 49 528K 964K 1.21M 26.1M
460 System Pre 0.0% 0:05.57 2 113 181 2.50M 10.1M 10.6M 58.7M
273 ProcessVie 0.0% 1:34.51 2 72 56 1.66M 5.02M 4.66M 54.0M
267 Terminal 12.1% 0:30.26 4 83 67 2.31M+ 6.11M 6.48M+ 55.4M
266 Dock 0.0% 0:23.38 2 117 98 1.47M 3.44M 3.32M 46.8M
265 Finder 0.0% 1:09.59 3 97 406 14.9M 14.3M 22.1M 84.6M
264 pbs 0.0% 0:02.53 3 121 66 588K 1.36M 1.59M 15.6M
240 loginwindo 0.7% 1:06.71 2 92 75 1.99M 3.30M 3.45M 40.4M

and just for grins :-) here is the output from top for the Sun desktop running this web site and a number of mailing lists: :

last pid: 18824; load averages: 1.00, 1.03, 1.05 16:48:16
38 processes: 36 sleeping, 1 running, 1 on cpu
CPU states: 0.0% idle, 98.8% user, 1.2% kernel, 0.0% iowait,0.0% swap
Memory: 128M real, 46M free, 15M swap in use, 497M swap free
 
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
9997 seti 1 0 19 18M 16M run 824.4H 97.09% setiathome

I dare say that is as much as you'll need to know about SWAP until you run into some abused overloaded server that's behaving poorly and you've got to figure out if it is really in need of more memory, or there is some other reason for it performing poorly.

 

Date Created: Tue Aug 14, 2001 05:02:55 PM US/Eastern with OS X 10.1.x and Solaris 8