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 (41)
:: 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.
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. |
QuicksearchCategoriesSyndicate This Blog |
Powered by s9y
