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

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

Comments

  1. missing xupport says:

    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!

    • Remon Pel says:

      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.

      • missing xupport says:

        oh! on sleepimage, i was wondering for lion, too.

        thanks for the pre-response!

        • Remon Pel says:

          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.

  2. Rob says:

    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.

    • Remon Pel says:

      Hi Rob,
      I did not remove your comments, I just haven’t gotten around to approving them. That’s all.

      Couldn’t get it to work on Mountain Lion. Carefully checked and re-checked my editing.

      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 :)

      I tried this on 10.8.2 and it failed.
      I carefully copied the text and pasted it into the plist file. I also tried again, using Xcode to do the editing and still it did not work.
      Not sure if I have missed something.

      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 :)

  3. Rob says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>