Home » Questions » Computers [ Ask a new question ]

Moving the swapfiles to a dedicated partition in Snow Leopard

Moving the swapfiles to a dedicated partition in Snow Leopard

I have been able to move Apple's virtual memory swapfiles to a dedicated partition on my hard drive up until now. The technique I have been using is described in a thread on forums.macosxhints.com.

Asked by: Guest | Views: 114
Total answers/comments: 5
Guest [Entry]

"NOTE: See (also) a corrected/improved answer in the question itself.

Following solution worked for me:

Open a terminal and backup com.apple.dynamic_pager.plist which you're going to change in a second:

$ cd /System/Library/LaunchDaemons
$ sudo cp com.apple.dynamic_pager.plist{,_bak}

convert binary plist to xml:

$ sudo plutil -convert xml1 com.apple.dynamic_pager.plist

and open it with your favorite text editor

$ sudo vim com.apple.dynamic_pager.plist

it'll look something like this:

1 <?xml version=""1.0"" encoding=""UTF-8""?>
2 <!DOCTYPE plist PUBLIC ""-//Apple//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
3 <plist version=""1.0"">
4 <dict>
5 <key>EnableTransactions</key>
6 <true/>
7 <key>HopefullyExitsLast</key>
8 <true/>
9 <key>Label</key>
10 <string>com.apple.dynamic_pager</string>
11 <key>OnDemand</key>
12 <false/>
13 <key>ProgramArguments</key>
14 <array>
15 <string>/sbin/dynamic_pager</string>
16 <string>-F</string>
17 <string>/private/var/vm/swapfile</string>
18 </array>
19 </dict>
20 </plist>

In line 17 modify /private/var/vm/swapfile (e.g. /Volumes/partition2/swapfile), save and close your editor ("":x"" will do both in vim).

convert the plist file back to binary:

$ sudo plutil -convert binary1 com.apple.dynamic_pager.plist

After rebooting your Mac you should find the swapfiles in the directory you specified.

If you run into any problems you can restore the backup you created in the first step with:

$ cd /System/Library/LaunchDaemons
$ sudo cp com.apple.dynamic_pager.plist{_bak,}"
Guest [Entry]

"just a question: why not just editing the .plist file adding wait4path, instead of using the intermediate dynamic_pager_init ?

something like this:

EDIT: as explained in the comment by e.James and my following comment, the immediately following XML block is not good, both because there's an error (missing &&) and because only the first argument of the array ProgramArguments it's parsed as the program to run!but.. (scroll down)

...
13 <key>ProgramArguments</key>
14 <array>
15 <string>/bin/wait4path</string>
16 <string>/Volumes/Swap/</string>
17 <string>/sbin/dynamic_pager</string>
18 <string>-F</string>
19 <string>/Volumes/Swap/.vm/swapfile</string>
20 </array>
...

end of the wrong xml block

this XML block should work instead:

...
13 <key>ProgramArguments</key>
14 <array>
15 <string>/bin/bash</string>
16 <string>-c</string>
17 <string>/bin/wait4path /Volumes/Swap/ && /sbin/dynamic_pager -F /Volumes/Swap/.vm/swapfile</string>
18 </array>
...

please keep in mind that i still had not enough time to safely try this setting, but i tried to run various other shell commands launched in the same way, and everything should work as expected

How it works:

base: executing wait4path /path && command means that command is run only if wait4path ends and exits without errors, and this happens only when /path is an available path, so we can safely tell dynamic_pager to use that volume for swapfiles

1) as written in launchd.plist manpage, the keys Program and ProgramArguments are mapped to an execvp call, that means that everything but the first string in the array is treated as an argument for the first string in the array, the program to run;

2) as written in bash manpage, there is a bash -c <string> option to run the string passed as commands

1+2 = 3) what happens using this command line in a launchd plist ??

/bin/bash -c ""wait4path /Volumes/Swap/ && /sbin/dynamic_pager -F /Volumes/Swap/.vm/swapfile""

/bin/bash is the program to run, -c is the first argument and the double quoted string is the second argument

I guess it should work exactly as your solution, without the intermediate script: launchd will start the service, that will wait for the given path and then launch dynamic_pager..

Please note that:
* the string that you want to execute should be double quoted if you run bash -c in Terminal, but it's not double quoted in the plist file! (i guess because it's already declared as a string with the proper tag)
* the two & in the string must be changed to & in the plist file

PS: as always, proceed at your own risk, i take no responsibility for problems you may have using this setting !

thanks for sharing your work with us"
Guest [Entry]

"Maybe we can use Xupport to do those dirty job for us :) http://www.applicorn.com/

Virtual Memory Optimizer:

Change the swap files location
In Mac OS X the virtual memory information are stored in the so called ""Swapfiles"". Because the swapfiles are the most interactive system files, it makes sense to put them on a separate partition. It is recommended to store the swap files on the first partition of your fastest internal hard disk. The recommended minimum partition size should be about 3 or 4 times lager than the physical built-in memory size (e.g 1 GB physical memory = 4 GB swap partition).
Recommendations and instructions for an optimal system performance:

1.
Re-partition your hard disk with a swap volume as FIRST partition (using the Mac OS X Install DVD).
WARNING: RE-PARTITIONING A HARD DISK WILL EREASE ALL EXISTING DATA!

2.
Restore your system data or install new system on the system partition.

3.
Boot from the system partition.

4.
If you want your swap volume to be invisible to the Finder:
• Launch Xupport and choose ""Settings""
• Enable ""Show hidden files and folders"" and restart the Finder
• Rename the swap volume from ""swap"" to "".swap"" (The dot makes it invisible to the Finder)
• Disable ""Show hidden files and folders"" and restart the Finder again

5.
Select the new swap partition under ""Swap Storage Volume"". Then, press the ""Set"" button to apply the new swap file location settings (Restart required).

BTW, it works for me. Snow Leopard 10.6.2 @ Macbook Pro 2.4GHz, 4GB Ram, 500GB HD"
Guest [Entry]

"This is most likely an unwanted answer (since I can't comment after Diago), but why really do you insist this will give small performance gains? I've went through a discussion on apple forums and conclusion was that this is not a good idea at all. And I was very resistant on abandoning it. Could you come up with data proving that at least for yourself, or is it just a ""feeling""?

From every time I used swap even on linux, back 10 years ago, and nowadays on ubuntu, I could never see improvements on performance. My reason for wanting it was to prevent issues with free space on OSX and, on linux, for being able to hibernate. That's all swap is to me.

But I've never really did deeper research either on my own or in the interwebs."
Guest [Entry]

"This isn't an answer, but perhaps a very useful follow-up supplement. Apple provides a free plist editor here PlistEdit Pro. It allows you to edit these files safely. Looks as if you could just change the argument with the value /private/var/vm/swapfile to something like /Volumes/OtherDrive/vm/myswapfilename to move the swap files somewhere else...

Again I have not tested this..."