When I run htop (on OS X 10.6.8), I see something like this :
1 [||||||| 20.0%] Tasks: 70 total, 0 running 2 [||| 7.2%] Load average: 1.11 0.79 0.64 3 [|||||||||||||||||||||||||||81.3%] Uptime: 00:30:42 4 [|| 5.8%] Mem[|||||||||||||||||||||3872/4096MB] Swp[ 0/0MB] PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command 284 501 57 0 15.3G 1064M 0 S 0.0 6.5 0:01.26 /Applications/Firefox.app/Contents/MacOS/firefox -psn_0_90134 437 501 57 0 14.8G 785M 0 S 0.0 4.8 0:00.18 /Applications/Thunderbird.app/Contents/MacOS/thunderbird -psn_0_114716 428 501 63 0 12.8G 351M 0 S 1.0 2.1 0:00.51 /Applications/Firefox.app/Contents/MacOS/plugin-container.app/Contents/MacOS/ 696 501 63 0 11.7G 175M 0 S 0.0 1.1 0:00.02 /System/Library/Frameworks/QuickLook.framework/Resources/quicklookd.app/Conte 38 0 33 0 11.1G 422M 0 S 0.0 2.6 0:00.59 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framewo 183 501 48 0 10.9G 137M 0 S 0.0 0.8 0:00.03 /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder
How can I have Processes using Gigabytes of VIRT memory and still 0MB of Swap used ?
Best Answer
VIRT
does not have anything to do with used memory (virtual or otherwise), but with used address-space, which is not as related as you might think.Modern operating systems (including OSX) have a feature called demand paging which works by telling the operating system to map a certain region of virtual address space to a file (such as a shared library/DLL). It is only when a program tries to read those virtual addresses that the file is loaded into memory.
If those shared libraries are in fact shared, then the operating system will actually share the physical memory across multiple processes; That is to say that a great deal of those tens of gigabytes are not only file-backed libraries, but also the same file-backed libraries.
Further, if scratch memory is required (for data; settings, bitmaps, sounds, etc), and there aren't any physical pages unused, the operating system will actually discard the contents of these file-backed regions and give them to your application. If those pages are needed again, the operating system can simply reload them from disk.
Swap (
SWP
) is a special file-backed region for that scratch memory. Creating swap-space will allow the operating system to move that scratch memory to the disk instead of (utilised by more running process) shared libraries, generally improve performance, and perhaps most surprisingly of all, produce less swapping than having no swap space at all.