Remons TechNotes

Virtual Memory Configuration

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;

Shell commandsysctl vm.swapusage

A line line this will tell you all you need to know;

Shell outputvm.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.

Shell commandsudo 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;

Shell commandsudo defaults write /Library/Preferences/com.apple.virtualMemory DisableEncryptedSwap -boolean no

If you want to disable Virtual Memory completely, issue this command;

Shell commandsudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist

And to re-enable it again, issue this one;

Shell commandsudo 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.

Shell commandcd /System/Library/LaunchDaemons && sudo cp com.apple.dynamic_pager.plist{,_bak}

Now, convert the PLIST to the XML format so it is editable

Shell commandsudo plutil -convert xml1 com.apple.dynamic_pager.plist

Then, edit the file

Shell commandsudo nano -w com.apple.dynamic_pager.plist

The file looks like this;

Editor contents<?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/ &amp;&amp; /sbin/dynamic_pager -F /Volumes/Swap/swapfile</string>

Finally, convert the PLIST back to Binary;

Shell commandsudo 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

Shell commandsudo pmset -a hibernatefile /Path/to/New/sleepimage

Original location, in case you want to revert;

/var/vm/sleepimage
Exit mobile version