A few command lines for managing the Virtual Memory on your Mac.
Please note; if you have a regular amount of memory, like 4 GB or 8 in recent models, you might want to consider NOT turning off Virtual Memory. But if you have ample RAM, give it a try, you will notice a big speed improvement. Why? Thats because Virtual Memory is ALWAYS used, even if you don’t really need it.
Choose OSX Version: 10.6 | 10.7 | 10.8 | 10.9 | 10.10 | 10.11
To check the current use of Virtual Memory, issue this command;
sysctl vm.swapusage
A line line this will tell you all you need to know;
vm.swapusage: total = 2048.00M used = 930.34M free = 1117.66M (encrypted)
Here you see my system has 2 GB of Virtual Memory, of which almost 1 GB is used. My system has 4 GB of real RAM, so theoretically, with 6 GB RAM, I should be able to use my system without VM. Unfortunately, it’s not really that simple, but the idea is simple enough.
You will also notice ‘Encrypted’ in the output. That’s a great security feature; an unencrypted memory dump would leave my system open to exploits. But if you insist, you can turn off encryption.
sudo defaults write /Library/Preferences/com.apple.virtualMemory DisableEncryptedSwap -boolean yes
After a reboot the new setting will be used.
To re-enable encryption, issue the reverse;
sudo defaults write /Library/Preferences/com.apple.virtualMemory DisableEncryptedSwap -boolean no
If you want to disable Virtual Memory completely, issue this command;
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
And to re-enable it again, issue this one;
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
To move the Virtual Memory file to a different location (recommended if you have an SSD as primary disk and also have a regular HDD in your machine; move the file to the HDD)
First, make a backup of the current settings file.
cd /System/Library/LaunchDaemons && sudo cp com.apple.dynamic_pager.plist{,_bak}
Now, convert the PLIST to the XML format so it is editable
sudo plutil -convert xml1 com.apple.dynamic_pager.plist
Then, edit the file
sudo nano -w com.apple.dynamic_pager.plist
The file looks like this;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnableTransactions</key>
<true/>
<key>HopefullyExitsLast</key>
<true/>
<key>Label</key>
<string>com.apple.dynamic_pager</string>
<key>OnDemand</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/sbin/dynamic_pager</string>
<string>-F</string>
<string>/private/var/vm/swapfile</string>
</array>
</dict>
</plist>
Determine the new location;
If the location is on the boot partition, you only need to change the path. Not very useful, because it doesn’t do any good; performance stays the same.
To move the file from /private/var/vm/swapfile to /swapfile, change
<string>/private/var/vm/swapfile</string>
to
<string>/swapfile</string>
When moving the swapfile to a different partition or drive, you’ll need to add a little protection; your system will have to wait till the target path becomes available. Fortunately, there’s a command for that; wait4path.
To move the file to /Volumes/Swap/swapfile, which is, obviously, on a partition called Swap
change the lines
<string>/sbin/dynamic_pager</string>
<string>-F</string>
<string>/private/var/vm/swapfile</string>
to
<string>/bin/bash</string>
<string>-c</string>
<string>/bin/wait4path /Volumes/Swap/ && /sbin/dynamic_pager -F /Volumes/Swap/swapfile</string>
Finally, convert the PLIST back to Binary;
sudo plutil -convert binary1 com.apple.dynamic_pager.plist
Now, restart your mac. The new file will be created automatically, but you might have to remove the old file yourself.
Last VM-ish option: you might want to move the sleep-image, you can do this with the command
sudo pmset -a hibernatefile /Path/to/New/sleepimage
Original location, in case you want to revert;
/var/vm/sleepimage
thank you so much for this.
a few questions:
1) does this work in mountain lion?
2) do you have any advice along these lines for moving the sleep image as well?
thanks!
In reply to both questions; I don’t have access to Mountain Lion yet so I cannot say. Of course, once I do I will update the post.
oh! on sleepimage, i was wondering for lion, too.
thanks for the pre-response!
Quite welcome.
I have been able to disable Virtual Memory on Mountain Lion with above commands, moving the pager file also works.
I am ASSUMING the encryption options are available in ML as well, but I have not tested it yet.
In regard to the sleep image; this post didn’t cover that, did it :P Well, now it does, and it’s 10.8 tested.
I puzzled that you removed my previous posts.
However, I solved my problem of this not working by:
chown root:wheel /Volumes/Swap
chmod 755 /Volumes/Swap
Perhaps you might want to add this footnote for others.
Cheers.
Hi Rob,
I did not remove your comments, I just haven’t gotten around to approving them. That’s all.
Can you be a bit more specific? What did not work? turning on/off encryption? moving the sleep-image? moving the swap-image? It’s quite hard to give you an answer without details :)
Again, I need more details. I know now you’re coping/pasting something, so that would probably be the com.apple.dynamic_pager.plist file.
I tested it myself just now, on 10.8.2, and found no problems.
To respond to your last comment; One should not have to set the permissions and ownership to Root 755, root is allowed to write anywhere. Of course, I’ll leave the comment here, just in case :)
My apologies for the lack of clarity.
Moving the swap didn’t work until I had done:
chown root:wheel /Volumes/Swap
chmod 755 /Volumes/Swap
I was a bit perplexed by this myself, especially as I had been logged in as root when I was modifying the plist file. I used iPartition to create the Swap partition, so don’t know if it was an artifact of that operation.
Thanks for your patience.
Hi,
I’m trying to move my swapfile to another drive in my macbook
I think I’m being a little slow, but when I get as far as here
I don’t know where to input the command, I mean that having entered
sudo nano -w com.apple.dynamic_pager.plist
I don’t have a command prompt to type that at because I’m view the file itself, what do I need to do to get it back?many thanks
not quite understanding where you are stuck, but if you entered “sudo nano ….’ you are editing the file. Save and quit nano by pressing CTRL-X and answering the question to save with ‘yes’. Nano will then exit and bring you back to the shell. There you can enter the command to convert the plist back to binary.
Good luck!
thank you so much for this, a simple question :does this work on 10.11 (el capitan) ?regards
a simple question with a difficult-to-find answer. VirtualMemory Encryption can not be turned off in El Capitan. The PageFile settings are the same.