Apple - Mac OS X is not creating a swap file

I had the same problem when I installed High Sierra on external SSD.

Volume disk3s4 647DA4A9-7E85-4523-A4D2-F0392D3789D4
        ---------------------------------------------------
        APFS Volume Disk (Role):   disk3s4 (VM)
        Name:                      VM (Case-insensitive)
        Mount Point:               Not Mounted
        Capacity Consumed:         4294987776 B (4.3 GB)
        FileVault:                 No

Solution:

  1. Create a plist file as root user and put it in /Library/LaunchDaemons/ folder. It has to be written in reverse domain notation like this:

    /Library/LaunchDaemons/local.mountdisk3s4.plist
    
  2. Just copy this xml data in your plist file and change the name of APFS VM Volume with yours.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
         <key>Label</key>
         <string>THE NAME OF FILE</string>
         <key>ProgramArguments</key>
         <array>
              <string>/sbin/mount_apfs</string>
              <string>YOUR APFS VOLUME</string>
              <string>/private/var/vm</string>
         </array>
         <key>KeepAlive</key>
         <dict>
        <key>SuccessfulExit</key>
        <false/>
         </dict>    
    </dict>
    </plist>
    

    In my case it looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
         <key>Label</key>
         <string>local.mountdisk3s4</string>
         <key>ProgramArguments</key>
         <array>
              <string>/sbin/mount_apfs</string>
              <string>disk3s4</string>
              <string>/private/var/vm</string>
         </array>
         <key>KeepAlive</key>
         <dict>
        <key>SuccessfulExit</key>
        <false/>
         </dict>    
    </dict>
    </plist>
    
  3. Reboot your Mac


I have had the same problem running High Sierra (and Mojave since) off an external SSD. I haven't tried Glorfindel♦'s suggestion to wipe the disk to Sierra on HFS+ before restoring to High Sierra, which seems like a lot of work.

I have, however, been using my own launch daemon with a bash script since January, similar to chrisgooley's solution, however, my script also checks which volume ID to mount. I thought I'd share my solution with the dynamic check for the correct volume ID.

Initially, I hardcoded the volume ID as well but this was problematic because whenever the system booted with additional drives attached, the volume ID would change the swap volume would fail to mount.

My script and daemon are below:

mountvm.sh

#!/bin/bash
# Mount the APFS VM volume if it isn't already mounted

VM_VOLUME=$(/usr/sbin/diskutil list | grep "VM" | awk '{ print $7 }') 
# echo "VM Volume is $VM_VOLUME"
for i in {1..5}
do
    if [ ! -e /private/var/vm/sleepimage ]
    then
#       echo "$(date "+%a %d/%m/%Y %I:%M:%S%p") > VM volume not yet mounted..."
#       echo -n "$(date "+%a %d/%m/%Y %I:%M:%S%p") > "
        /usr/sbin/diskutil mount -mountPoint /private/var/vm/ $VM_VOLUME
        break
    else
#       echo "$(date "+%a %d/%m/%Y %I:%M:%S%p") > VM volume already mounted..."
        if [ $i -lt 6 ]
        then
#           echo -n "$(date "+%a %d/%m/%Y %I:%M:%S%p") > Confirming in "
            for count in {2..1}
            do
#               echo -n "$count min..."
                sleep 60
            done
            echo
        fi
    fi
done

exit 0

com.local.mountvm.plist

<?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>Label</key>
    <string>com.local.mountvm</string>
    <key>ProgramArguments</key>
    <array>
        <string>[/path/to/script]/mountvm.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Some notes:

Change the [/path/to/script] in the plist to where ever you place the bash script.

Remove the # on the echo lines in the bash script to troubleshoot (you'll need to run the script in Terminal with sudo or specify a stdout path in the plist).

The script checks whether /private/var/vm/sleepimage exists. If it doesn't, then the swap volume isn't mounted and tries to mount the correct volume. If it does, it will check again four more times in two minute intervals before exiting. The reason I added this was because I found if I simply tried to mount the volume as soon the daemon loaded, it would fail.


The dedicated VM APFS volume is not mounted properly:

+-> Volume disk4s4 5DE0EA6B-CA57-4226-B038-2E256FCC5B98
    ---------------------------------------------------
    APFS Volume Disk (Role):   disk4s4 (VM)
    Name:                      VM (Case-insensitive)
    Mount Point:               Not Mounted
    Capacity Consumed:         2147504128 B (2.1 GB)
    FileVault:                 No

It should be mounted to the Mount Point /private/var/vm.

Entering mount in Terminal should reveal something like:

...
/dev/disk4s4 on /private/var/vm (apfs, local, noexec, journaled, noatime, nobrowse)
...

The reason is unclear. At least some swap files have been created in the past because 2.1 GB (= two swapfiles à 1 GiB) are consumed by VM.


A temporary workaround is to specify another swap file directory. After disabling SIP, this can be accomplished by modifying the file /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist with sudo nano ... or LaunchControl.

Original:

<?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>Label</key>
    <string>com.apple.dynamic_pager</string>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <false/>
    </dict>
    <key>POSIXSpawnType</key>
    <string>Interactive</string>
    <key>ProgramArguments</key>
    <array>
        <string>/sbin/dynamic_pager</string>
    </array>
</dict>
</plist>

Mod:

<?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>Label</key>
    <string>com.apple.dynamic_pager</string>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <false/>
    </dict>
    <key>POSIXSpawnType</key>
    <string>Interactive</string>
    <key>ProgramArguments</key>
    <array>
        <string>/sbin/dynamic_pager</string>
        <string>-F</string>
        <string>/vm/swapfile</string>
    </array>
</dict>
</plist>

If the directory /vm doesn't exist, create it:

sudo mkdir /vm
sudo chmod 755 /vm

Reboot your Mac afterwards. Enable SIP again!


In my opinion this is related to the 10.13.3 Supplemental Update. At least in my various High Sierra VMs additional folders /vm were created - each containing one abandoned swapfile0. The actual swap directory is /private/var/vm -> disk1s4 (APFS VM volume) though - tested with sudo memory_pressure -l critical in Terminal.

I have to further investigate this.


To really fix the problem removing and re-adding the somehow broken VM APFS volume should help:

  • Restore the default com.apple.dynamic_pager.plist file
  • Check whether the folder /private/var/vm exists
  • Boot to High Sierra Recovery Mode
  • Open Terminal in the menubar > Utilities and enter diskutil ap list to get the APFS details
  • Remove the APFS VM volume:

    diskutil ap deleteVolume <av_vmUUID> #<av_vmUUID>: UUID of the APFS Volume with the VM role
    

    In your case av_vmUUID is 5DE0EA6B-CA57-4226-B038-2E256FCC5B98 so:

    diskutil ap deleteVolume 5DE0EA6B-CA57-4226-B038-2E256FCC5B98
    
  • Add an APFS VM volume:

    diskutil ap addVolume diskX APFS VM -mountpoint /private/var/vm -role V
    

    with diskX: APFS Container Reference of the container with the UUID 6BE5FDB5-A68F-4CBF-A404-68AE73E61C10 shown in the diskutil ap list (probably disk3, disk4 or disk5)

    The volume will be created but it won't get mounted because the specified mountpoint doesn't exist in the base system of the Recovery Mode!

  • Reboot your Mac and first check if VM is mounted to /private/var/vm with mount. The test it with sudo memory_pressure -l critical.