Killing Mac OS X Swapping : How To Disable dynamic_pager
If like me you cannot deal with the swapping in OS X, and if you have tried everything possible, even not to launch dynamic_pager (and you've seen that you're system is not usable anymore (on my mac the 2Gb was detected but the system was refusing to use more then 1Gb Actually, the original source code of dynamic_pager can be found at http://www.opensource.apple.com/darwinsource/10.5.2/system_cmds-433/dynamic_pager.tproj/dynamic_pager.c. You will need an apple developper account (that's free) do download it. If you know how to compile this, please add a comment to this post. After a bit of hacking, (removing cryptic junk, calls to private code of the kernel that is not accessible for the public, hacks to make it compile and commented a macx_swapon call), here is the modified dynamic_pager.c. Once you downloaded it it can be compiled by using this awful command : gcc -o dynamic_pager -no-cpp-precomp -DNO_DIRECT_RPC -framework CoreFoundation -framework IOKit -lSystem.B -R -DNO_DIRECT_RPC dynamic_pager.c -I/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers -I/Developer/SDKs/MacOSX10.4u.sdk/usr/includeThen (after having done a backup of the regular dynamic_pager) copy your brand new dynamic_pager to /sbin. Reboot and enjoy : it will only create a single swap file (64Mb), no less, no more. I'm currently running it and it seems to work really fine. All my memory is used, not only the first 1Gb. The Inactive Memory is high as usual, but the single swap file is only marginally used. The code posted above is just a big hack, when I will have more time I will clean up the code so that it will be a bit more understandable. Enjoy.
Comments (78)
:: Trackbacks (2)
Trackbacks
links for 2008-04-03
cestdelamerde.com (tags: fun humour) old story about going to greenland (tags: greenland vacances) Killing Mac OS X Swapping : How To Disable dynamic_pager - cestdelamerde.com Very good hack to improve how swapping is handeld in leopard. (tags: macintosh memory...
Weblog: Ludovic's weblog
Tracked: Apr 03, 07:36
Mac OS X Leopard - dynamic_pager ?????
Mac OS X 10.5.1 Leopard ?????????? OS X ??????????swapping. ??????????????????????????
Weblog: 404 Blog Not Found
Tracked: Aug 24, 23:57 Comments
Display comments as
(Linear | Threaded)
Philou - could you also post some diffs ?
No, but you can do the diff by yourself if you have the original dynamic_pager.c file
I'm currently testing another version of this hack to completely disable paging : in my current dynamic_pager.c file, there is no reference to macosx_swapon() anymore - it seems to work pretty well.
Heya, first of all, I'm also "suffering" like U about all this "prehistoric" way of managing modern resources and more specifically memory and HDD access.
I've implemented U'r hack, and for now I'm so excited as a little boy about to open a christmas present...in deed it is. I've posted many rants and hacks in my blog (in Spanish) about how IT should manage the menory and all this, and I completly share U'r thoughts! I'm interested in the last hack U implemented, the explanation and how it fared, and maybe if it is available... I also have some more thoughts about how memory management SHOULD be implemented for ACTUAL times, so hit me up by email when U have time CIAO! CeK!
You can download the diff here : http://cestdelamerde.com/uploads/dynamic-pager.diff
s/^No/I do not have access to my mac and I will not have time to post the diff until next week/
Hi and thx again!
I've been experimenting, and doing some (quite) harsh things to my poor MBP 3GB... I've modified the dynamic_pager on my own, and no swap is generated. The system just FLYS!!! Need to do some more experimenting and maybe try and fuse the 2 versions. The idea behind is that once you overwhelm the system's memory, and before just crashing the system (coz of out of memory), the dynamic_pager starts swapping automagically. when I have some free time I'll try to write this and do some more experiments, and hopefully let U know :p Thx again for being the pioneer! CIAO! CeK!
Thanks for the great hack. In spite of 4GB RAM I started getting swaps (maybe having too much fun w/ VMWare Fusion).
I wrote a Makefle to automate that. It fetches and build Universal Binary dynamic_pager. "make install" backs up the original. http://www.dan.co.jp/cases/macosx/leopard/hacked_dynamic_pager/Makefile.txt Hope you like it. Dan the Leopard Tamer
Hi philou,
Thanks, that's a really interesting approach. I'm running Fusion the whole day and my mbp with 4gb ram gets usually unnecessary slow because of swapping. Your solution brought a big boost, but unfortunately after a day it's slowing down again... Would it be possible to get rid of the last 64mb swap as well?
well... just try to comment the line 451 (the call to macx_swapon)
does this work in 10.5.5 ? im getting tons of errors and warnings.
Interesting hack. I've 4 gigs of RAM installed on my MBP, but for some reason OS X likes to create big swap files (about 4 gigs reported by Activity Monitor). That efficiently slows down the whole system, as swapping to 5400 RPM disk is freezing slow.
While "unnecessary" swapping may be ok with fast desktop disk(s) and actually speed-up things, it seems that swapping in OS X is not really tuned for slow laptop disks. I'm pretty sure the system would still swap even if I installed 8 gigs of RAM (which is not possible, though). I wish OS X had "swappiness" parameter, like Linux does. Anyway, this really sucks, because I know 4 gigs of RAM should be more than enough for apps I run. Not sure if I'm brave enough to try your hack. Don't want to end up with unbootable system
I've never encountered any issue at boot time with this method, the only (big) issue, is when your system is running out of memory, the whole os hangs...
Is there a 10.4.11 version? My system hung up and would not boot when I tried it using Xcode 2.5. Of course, I also had a large number of warnings building the executable starting with -R not being a valid option for gcc. Should it build cleanly?
I'm sorry, Paul-B but I cannot help you on that since I'm runnning Leopard on my mac.
Hi! Thanks for making your dynamic_pager hack public.
I've compiled and installed it and seems to work, but there's one weird thing: both "sysctl vm.swapusage" and the VMTester (from http://hayne.net/MacDev/VMTester/) report 128MB swap, but there's only a single 64MB swapfile in /private/var/vm. :-o I cannot explain this ... any ideas?
That problem got solved by itself: after the next reboot all tools reported only 64M swap and there was only a single 64MB swapfile present. So that's normal now.
However I've another issue. If I understand it right, then "Active memory" in Activity Monitor (and top) should show the amount of memory that is used by currently present processes. However if I sum up the RSIZE column either in Act. Monitor or top, I get less than half the amount of Active Memory. So my best guess is that Active Memory covers something else besides memory allocated by current processes.
Unfortunately, there is no documentation available concerning mac os VM, so I guess you're right
i assume what you mean by out of memory is when inactive memory are getting low too.
leopard's VM is strange that it just swap out inactive memory when it is low of memory. it is possible to just stop it from swapping until inactive memory are gone?
When I say "out of memory", you must read: out of freeable (not sure this word exists
Sure this VM is strange, the inactive memory also seems to hold some cached data from the disk, so the system is swapping out some cached data to the filesystem : this is a dumb behaviour. Eg: you launch firefox, then quit firefox, instead of freeing the memory used by code page of FF, the system will mark them as inactive, then if you launch FF again, it will be very fast, since you will not have to read from the disk those pages. This is quite smart actually, unfortenately, the VM code that is responsible for swapping pages to the disk seems not to be aware of what is inactive memory. Also, I've never seen a Mac OS X system running with 0 bytes (or at least very low value) of inactive memory even if the system is under a heavy charge. The system by construction seems not to be able to free all inactive memory. I've no idea why. I've no time to read the kernel code, also it would be pointless as nobody on earth (at least if you do not work for apple) can recompile a modified kernel to run Mac OS X... (huge parts of the code like Rosetta have been removed from the opensource version distributed by apple, it won't compile... or if you manage to compile it nothing will work on the mac).
i did some test before and i think leopard will actually cache the code as active memory. i didn't see any increase of inactive when i launch and quit applications(and it does release some active memory too). so i really have no idea how apple define inactive.
i've a 4G macbook myself and my system will have to reboot once per week (i usually only suspend) as my swap file can easily get to 4G+. i guess i'll have to give your patch a try to see when leopard think it's out of freeable memory ;P problem is my only macbook is my primary system
i've been trying your patch for few hours. it works great! thanks for the patch.
leopard seems to able to free up some disk cache in active memory when free memory gets low. i actually think inactive memory is just some fragment that can't be release and reused(performance concern maybe) let's see how it goes after a few months!
I'm glad you enjoy this little patch
Just be aware that if you consume the whole amount of memory of the mac, the machine will likely hang... As you have 4G of ram, this should never happen however. I'm running this hack since one year on my mac. I have a 2G mac book pro, my mac just freezed 4 or 5 times during the past year. (It happened when I tried to launch Firefox+Adobe Lightroom+Adobe Photoshop (loaded with at least 5 big pictures)+Skype+Thunderbird+Transmission at the same time). So it's definitely worth a try
hehe what i mean by trying was opening all my vmware vm and such. i actually hang my machine few times already for testing purpose.
i was thinking if inactive memory belong to disk cache it would be possible to do some more hack on dynamic_pager to enable the swap only when inactive+free below a certain level. however it's not the case. i actually know some of my python application can result in some left out in inactive memory sometimes after quit. it should be and most likely is memory fragment problem or even memory leak or whatever, i have no idea. that leave us no idea how many disk cache actually in active memory. let's hope snow leopard will improve it. i really think it's dumb to swap disk cache. and yea this hack is pretty safe and solid i can say. leopard will release disk cache itself from active memory if the swap turned off. the only problem is the misleading inactive memory by apple. inactive memory will never release when it's out of memory. but inactive memory does reclaim sometimes when u quit some other apiplcation(that's why i think it's memory fragment problem) but actually i am not an expert so maybe someone good at it should show us some lights.
after 2 weeks uptime (no shutdown, only suspend for sure!) my leopard finally hangs with this patch even with 4G ram.
as i can remember my inactive memory reach 1.4G when i checked few days ago. i've also confirm that disk cache will does into both inactive/active memory, say cp on terminal will cache as inactive, loading an application first time will cache it as active and etc anyway, nice patch and it works great for me. hope snow leopard can improve its memory management.
Could someone please drop the great compiled dynamic_pager for Leopard 10.5.x somewhere for code-dumbass like me to download. Please specify version/platform if more than one uploaded like different kernels. Thanks a ton.
I have a shell script that just sends inactive ram to though the roof. The script is a batch mp3 encoder and calls the LAME library over and over again as it iterates over directories of aiff files. Is OS X caching the same LAME code into inactive ram over and over? Would the modified dynamic pager help in this situation?
The only thing that matter is the ammount os swap space used by the VM. The modified dynamic_pager will solve this issue by not allocating any swap space.
But your scripts will still consume inactive ram. This should not be a problem anyway (when the system will run out of free memory, it will likely flush this inactive ram...)
i've some java and python application keep consuming inactive memory. for my experience leopard won't release those memory even it run out of memory using the dynamic_page patch.
let's try it out and let us know, the patch is quite solid and i am quite happy with it and my macbook running quite fast with the patch. the only backdraw is i have to reboot once a week to clean up those inactive memory. without the patch my macbook keep swapping within a few hours boot up and lag like hell
tested your patch
got my crash after few hours I usually am out of memory so swap is vital for me limiting to 64 is a problem the thing that I would like is a way to limit its usage
Try to modify dynamic_pager.c to forbid the dynamic_pager to allocate more than XXXX swap files...
For those having problems with Inactive Memory:
Just type purge in the Terminal, and your inactive memory will be freed.
Sure, but it does not flush the swap memory to the ram, so the mac stays unusable...
I didn't say anything about the swap file.
Purge should be used in addition to disabling your swap file, because even if you disable your swap file, which I've done, OS X will continue to eat up your RAM for its disk cache until it becomes unusable, which has happened to me often. When that happened to me, I had to reboot my computer, until I started to use purge. Now I never need to reboot my computer. OS X memory use is really deficient, but using purge alongside disabling or limiting your swap file, makes it usable again. I never have any slow downs anymore.
typing "purge" in Terminal returns "-bash: command not found"
does this mean it is in csh or tcsh instead of bash?
"purge" does not exist in my /usr/bin...
OK got it: it's part of the CHUD (developer) package at apple.com; installed CHUD & "purge" is now in usr/bin
with this patch when you run out of memory and it can't swap, leopard will release memory from disk cache. not all inactive memory are disk cache otherwise with this patch i won't need any reboot.
but purge is really useful for me, thanks for the info. i can actually avoid some reboot with it. i just purge with 1.4G inactive and it's now down to 900M.
anyone tried snow leopard beta? did they fix the swapping disk cache problem?
@alex:
i'm running Snow Leopard Preview from the WWDC DVD; doesn't build using Dan's makefile. i wasn't having any swap issues; i just wanted to turn off swapping to save disk space - i have a macbook air 1,1. i did it in leopard and i don't remember how.
i recently upgraded a cloned copy of my HD (10.5.7) to SL 10.6 to test it out. The problem i am encountering is that at start up its not recognizing the total ram intalled (1.5G).i am also getting unusually high active memory levels (600+)with no apps actually opend and once i open apps the faster it eats up my ram (1.5G). this did not happen with 10.5.7. I am using ifreememory app to keep freeing the memory but I am getting the feeling something is very wrong. Can anyone tell me what it is.
This whole thing is odd. I have a very recent macbook 13 inch (model just before they were redesignated pro), and it manages swap and virtual memory just fine. Right now I am running many applications and transcoding video. Out of 4 gigs, I have 1.45GB free, 122MB inactive, and 0 Bytes swap. I go for weeks without rebooting, and nothing ever slows down. Memory is always freed. I wonder if they've optimized for newer hardware?
Still works on Snow Leopard, since the source for 10.6 kernel is out. Compile with "-mdynamic-no-pic". However disabling the swapping is not such a good idea - have you run a disk benchmark? Copying files is much much slower since copying is done with paging. If you just want to free up some memory, just use "purge".
"purge" does not solves the issue, swapped memory is still on the disk after running the command ; having a lot of free memory really doesn't prevent macosx to swap...
I installed Snow Leopard last night, and immediately began tinkering with the latest dynamic_pager.c to fold in the hack. It was being obstinate, so I thought, "well, maybe I'll give it a chance before I go monkeying with it". I am pleased to report, one night's evidence may indicate they have tuned the aggressiveness of idle page-outs significantly:
greck@ghosts:~$ date; sysctl vm.swapusage Fri Aug 28 23:55:09 MST 2009 vm.swapusage: total = 128.00M used = 18.25M free = 109.75M (encrypted) greck@ghosts:~$ date; sysctl vm.swapusage Sat Aug 29 10:30:16 MST 2009 vm.swapusage: total = 256.00M used = 26.02M free = 229.98M (encrypted) Uptime is 14.5 hours with my normal app set, and that's what I get. For the first time ever with paging running as usual, Aperture sprung right back up from being hidden and was usable after 10.5 hours of idleness. So, probably too early to call this "fixed"... but it looks like that might just be the case.
Yeah ! It seems to be a good news.
I will give a try to leopard next week, I keep you posted about that
yeah i've been running snow leopard for a day and it seems to be much better than leopard. let's see how it goes.
update:
greck@ghosts:~$ uptime 13:38 up 2 days, 17:28, 4 users, load averages: 0.64 0.49 0.32 greck@ghosts:~$ sysctl vm.swapusage vm.swapusage: total = 1024.00M used = 818.70M free = 205.30M (encrypted) not exactly what I'd hoped for. BUT, some provisos: - active size hanging around 2 GB now vs. maybe 1.5 GB with 10.5 (consistent with the proliferation of 64-bit apps) - behaviourally, it seems to be paging more on-demand vs. hell of preemptively - Aperture still pops right back into action after having been hidden for hours so, evidence continues to gather for the idea that what we have in snow cat is a properly-functioning virtual memory system. I'd still love to have a checkbox to disable it in a correct/polite way, but 10.6 seems to be a huge improvement so far.
The article at http://www.macosxhints.com/article.php?story=20071121070305908 descries how to move the swap to a different location. However, it also says that if you move the swap to a non-existent location, then the swap is disabled.
It seems to work: $ date; sysctl vm.swapusage Thu Sep 24 15:41:51 PDT 2009 vm.swapusage: total = 0.00M used = 0.00M free = 0.00M
just... wow. the simplest solution ever. going to try that right now. I'm assuming dynamic_pager still starts and responds to messages about backing store, yes?
So, immediately I can tell you this is different than all previous attempts. 1) dynamic_pager ends up not running at all (presumably after gracefully realizing that /bad/apple/no/swapping doesn't exist); 2) most of an application's memory is now showing up as wired, which is different than before.
I'm hoping the change in reporting belies a change in backing store notifications or some such and as a result, this configuration will be free of the wonkiness I saw in 10.5 when you simply disabled dynamic_pager.
So simple, I'm hoping there is no side effects
I've seen before (Ok, on 10.4) some strange behaviour with a config were the dynamic_pager was not not running. I will try that this week end
Wow, this seems so simple. Could we get an update from any of you who've done this? Maybe we haven't gotten one because your computers won't boot now!
The safest way it to use "purge" command and the restart dynamic_pager
$ sudo purge $ sudo killall -HUP dynamic_pager this will make the swap usage minimum, and clear all /var/vm/swapfile* as necessary. This is safe and will not crash OSX.
if -U is the "user" parameter, what are the -H and -P parameters? (They don't show in my manpage...)
killall -HUP sends a SIGHUP signal to the process.
man kill is your friend.
well, reading man kill and man kill both together give only a hint of what you're saying, and neither alone shows -HUP written that way as a parameter: my man kill shows
" -signal_number A non-negative decimal integer, specifying the signal to be sent instead of the default TERM. ... 1 HUP (hang up)"
On my terminal, man kill also says :
"-signal_name A symbolic signal name specifying the signal to be sent instead of the default TERM. ... Some of the more commonly used signals: 1 HUP (hang up) 2 INT (interrupt) 3 QUIT (quit) 6 ABRT (abort) 9 KILL (non-catchable, non-ignorable kill) 14 ALRM (alarm clock) 15 TERM (software termination signal) "
thx - got it now - the relationship between "default TERM" and the later list of "signals" hadn't been clear
from my reading, it is not needed; but check the new man pages: if purge & kill were not deprecated in Snow it should work...
purge in snow leopard
root ~ # purge WARNING: Could not determine processor information. WARNING: Could not determine memory controller information. WARNING: Could not determine Operating System information.
that seems odd - check out the comments of Guppy, esp on 10/3, at - his script using purge works...
Guppy's comments are stored at:
sheesh - trying a 3d time:
http://forums.whirlpool.net.au/forum-replies-archive.cfm/1283629.html
Was anybody able to figure out why "purge" was not working?
I experienced the same problem after booting into 64bit SL.
10.5.8
I noticed that if you have 8GM of ram... the OS it self is not making any swap...
this won't even compile for me. i have the 10.4 framework installed... which version of gcc are you using? 4.2 gives me errors, 4.0 gives me warnings and no binary.
I'm curious how valid this still is in 10.6.x. I have 10GB of memory and I've been going nuts for years. The system seems to never utilize over 7-7.5G of memory at any given time and really starts to creep at the upper limits.
I'm wondering how this would effect my system and if its worth the effort on such a config. Thoughts?
I think it's safe to say don't bother... in fact, I don't bother anymore. after a zillion permutations, I can't find one sufficiently stable, especially with recent updates to 10.6 having made WebKit leak memory like a sieve.
So Can we change osx to flush disk cache(inactive mem) in time?
rather than manually purge ? This hack is great I found out some of the process, after closed the cache has not been released in time Over time, it will take a lot of memory
a simple ruby script will do the trick:
while true do sleep(60*60) #60 sec * 60 minutes puts "cleaning inactive memory #{Time.now}" exec 'purge' end save this code to a file (e.g. autopurge.rb) and run it in Terminal like 'ruby autopurge.rb' |
QuicksearchArchivesCategoriesSyndicate This Blog |
Powered by s9y
