How to free up a swap space manually without restart?

I'm on High Sierra on MacBook Pro with 16GB of RAM with SSD.

Got 15G of swap files, is there any method of freeing swap space/files without restart?

$ sysctl -a | grep swap
vm.swapusage: total = 15360.00M  used = 14468.75M  free = 891.25M  (encrypted)
vm.compressor_swapout_target_age: 0
vm.swapfileprefix: /private/var/vm/swapfile
debug.intel.swapCount: 0
$ ll -h /var/vm/swap*
-rw------- 1 root wheel 1.0G Apr 30 16:10 /var/vm/swapfile0
-rw------- 1 root wheel 1.0G Apr 30 17:16 /var/vm/swapfile1
-rw------- 1 root wheel 1.0G May 11 21:28 /var/vm/swapfile10
-rw------- 1 root wheel 1.0G May 11 21:46 /var/vm/swapfile11
-rw------- 1 root wheel 1.0G May 11 22:04 /var/vm/swapfile12
-rw------- 1 root wheel 1.0G May 11 23:00 /var/vm/swapfile13
-rw------- 1 root wheel 1.0G May 11 23:01 /var/vm/swapfile14
-rw------- 1 root wheel 1.0G May 11 23:02 /var/vm/swapfile15
-rw------- 1 root wheel 1.0G May  2 12:45 /var/vm/swapfile2
-rw------- 1 root wheel 1.0G May  4 14:02 /var/vm/swapfile3
-rw------- 1 root wheel 1.0G May  4 05:50 /var/vm/swapfile4
-rw------- 1 root wheel 1.0G May  8 11:05 /var/vm/swapfile5
-rw------- 1 root wheel 1.0G May  9 16:18 /var/vm/swapfile6
-rw------- 1 root wheel 1.0G May 10 03:02 /var/vm/swapfile7
-rw------- 1 root wheel 1.0G May 11 13:03 /var/vm/swapfile8
-rw------- 1 root wheel 1.0G May 11 21:07 /var/vm/swapfile9

I'd like to somehow drop these files without disabling dynamic_pager.


I've tried to:

  • run sudo purge (force disk cache to be purged);
  • quit all apps which I'm not using;
  • trick the system by creating dummy swap files before they're created:

    cd /var/vm
    sudo touch swapfile{0..20} 2>/dev/null; sudo chmod 000 swapfile{0..20} 2>/dev/null
    

    but it didn't work, as the system still creates/changes files despite of 000 permissions, weird.

For some reason, htop shows virtual memory as 532G, but I believe it's a bug, as top is showing less used.

htop vs top, PID USER PRI  NI  VIRT   RES S CPU% MEM%   TIME+  Command, swap, memory, CPU, macOS

Above htop/top shows only 2 tasks running, 445 are sleeping, CPU is not used that much, but kernel is busy with swapping between 15G of files.

I could restart it but after few days this problem repeating every week. Ideally, I'd like to kill and destroy (remove) swap files and start again on the same run, but macOS doesn't allow me to do it.

$ sudo rm -fr swapfile*
Password:
rm: cannot remove 'swapfile0': Operation not permitted

Are there any hacks available to force dynamic_pager to drop these files? I don't need that much.

Btw. These swap files are empty! See:

$ sudo strings swapfile*
swapfile0
swapfile1
swapfile10
swapfile11
swapfile12
swapfile13
swapfile2
swapfile3
swapfile4
swapfile5
swapfile6
swapfile7
swapfile8
swapfile9
/var/vm$ sudo gzip swapfile*
/var/vm $ ll -h *.gz
-rw------- 1 root wheel 1.6M Apr 30 16:10 swapfile0.gz
-rw------- 1 root wheel 1.6M Apr 30 17:16 swapfile1.gz
-rw------- 1 root wheel 1.6M May 11 21:28 swapfile10.gz
-rw------- 1 root wheel 1.6M May 11 21:46 swapfile11.gz
-rw------- 1 root wheel 1.6M May 11 22:04 swapfile12.gz
-rw------- 1 root wheel 1.6M May 11 23:00 swapfile13.gz
-rw------- 1 root wheel 1.6M May  2 12:45 swapfile2.gz
-rw------- 1 root wheel 1.6M May  4 14:02 swapfile3.gz
-rw------- 1 root wheel 1.6M May  4 05:50 swapfile4.gz
-rw------- 1 root wheel 1.6M May  8 11:05 swapfile5.gz
-rw------- 1 root wheel 1.6M May  9 16:18 swapfile6.gz
-rw------- 1 root wheel 1.6M May 10 03:02 swapfile7.gz
-rw------- 1 root wheel 1.6M May 11 13:03 swapfile8.gz
-rw------- 1 root wheel 1.6M May 11 21:07 swapfile9.gz

If these are empty, why macOS can't just remove them. I'm pretty sure that my slowness of the system (like load avg and including often mouse freeze) is caused by this constant creation and removal of these files, as I don't have any other processes which is using my CPU much.

For example, before writing this post, I had 13G of swap files (1G each), during my writing and pasting above output I ended up with 15G (by doing almost nothing), then macOS actually removed swapfile14 and swapfile15 (so 13G of swap) when writing this. Then removed swapfile13 when writing this sentence. Then created up to swapfile16 (16G) when writing this sentence. And removed swapfile16 again when writing this sentence. And so on. What a mess. If these are empty files, why macOS is keep creating them, when it didn't fill anything in there.


Other info:

$ sudo launchctl list | grep pager
-   0   com.apple.dynamic_pager
$ launchctl print system | grep dynamic_pager
               0      0     com.apple.dynamic_pager
$ ps wuax | grep dynamic_pager
# No running?!
$ launchctl dumpstate | grep -A20 com.apple.dynamic_pager
               0      0     com.apple.dynamic_pager
--
com.apple.dynamic_pager = {
    active count = 0
    path = /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
    state = waiting

    program = /sbin/dynamic_pager
    arguments = {
        /sbin/dynamic_pager
    }

    default environment = {
        PATH => /usr/bin:/bin:/usr/sbin:/sbin
    }

    environment = {
        XPC_SERVICE_NAME => com.apple.dynamic_pager
    }

    domain = com.apple.xpc.launchd.domain.system
    minimum runtime = 10
    exit timeout = 5
    runs = 1
    successive crashes = 0
    excessive crashing = 0
    last exit code = 0

Here is 1-second sample of WindowServer process, where at least half a second was spent on swap:

Command:         WindowServer
Parent:          launchd [1]
Duration:        1.01s
Steps:           10 (100ms sampling interval)
Active cpus:     8
Fan speed:       2159 rpm

  Thread 0x1ab              Thread name "VM_cswap_trigger"                      10 samples (1-10)         priority 91 (base 91)     cpu time 0.222s
  <IO tier 0>
 *10  call_continuation + 23 (kernel + 2098423) [0xffffff80004004f7] 1-10
   *10  ??? (kernel + 2882629) [0xffffff80004bfc45] 1-10
     *6  ??? (kernel + 2883102) [0xffffff80004bfe1e] 1-6
       *6  thread_block_reason + 175 (kernel + 2530911) [0xffffff8000469e5f] 1-6
         *6  ??? (kernel + 2534903) [0xffffff800046adf7] 1-6
           *6  machine_switch_context + 205 (kernel + 3578125) [0xffffff800056990d] 1-6
     *1  ??? (kernel + 2883122) [0xffffff80004bfe32] 7
       *1  ??? (kernel + 2885317) [0xffffff80004c06c5] 7
         *1  c_seg_do_minor_compaction_and_unlock + 303 (kernel + 2886351) [0xffffff80004c0acf] 7
           *1  c_seg_minor_compaction_and_unlock + 344 (kernel + 2887576) [0xffffff80004c0f98] (running) 7
     *2  ??? (kernel + 2883102) [0xffffff80004bfe1e] 8-9
       *2  thread_block_reason + 175 (kernel + 2530911) [0xffffff8000469e5f] 8-9
         *2  ??? (kernel + 2534903) [0xffffff800046adf7] 8-9
           *2  machine_switch_context + 205 (kernel + 3578125) [0xffffff800056990d] 8-9
     *1  ??? (kernel + 2883122) [0xffffff80004bfe32] 10
       *1  ??? (kernel + 2885317) [0xffffff80004c06c5] 10
         *1  c_seg_do_minor_compaction_and_unlock + 303 (kernel + 2886351) [0xffffff80004c0acf] 10
           *1  c_seg_minor_compaction_and_unlock + 633 (kernel + 2887865) [0xffffff80004c10b9] 10
             *1  kernel_memory_depopulate + 194 (kernel + 2976354) [0xffffff80004d6a62] 10
               *1  pmap_remove_options + 1124 (kernel + 3488404) [0xffffff8000553a94] 10
                 *1  pmap_remove_range_options + 1988 (kernel + 3485812) [0xffffff8000553074] (running) 10

  Thread 0x1ac              Thread name "VM_compressor"                         10 samples (1-10)         priority 91 (base 91)     cpu time 0.201s
  <IO tier 0>
 *6  call_continuation + 23 (kernel + 2098423) [0xffffff80004004f7] 1-6
   *6  vm_pageout_iothread_internal_continue + 1129 (kernel + 3134345) [0xffffff80004fd389] 1-6
     *6  vm_pageout_compress_page + 268 (kernel + 3135052) [0xffffff80004fd64c] 1-6
       *6  vm_compressor_pager_put + 160 (kernel + 2903904) [0xffffff80004c4f60] 1-6
         *6  vm_compressor_put + 1389 (kernel + 2900045) [0xffffff80004c404d] 1-6
           *1  WKdm_compress_new + 736 (kernel + 2092960) [0xffffff80003fefa0] (running) 1
           *1  WKdm_compress_new + 776 (kernel + 2093000) [0xffffff80003fefc8] (running) 2
           *1  WKdm_compress_new + 781 (kernel + 2093005) [0xffffff80003fefcd] (running) 3
           *1  WKdm_compress_new + 303 (kernel + 2092527) [0xffffff80003fedef] (running) 4
           *1  WKdm_compress_new + 748 (kernel + 2092972) [0xffffff80003fefac] (running) 5
           *1  WKdm_compress_new + 771 (kernel + 2092995) [0xffffff80003fefc3] (running) 6
 *1  vm_pageout_iothread_internal_continue + 0 (kernel + 3133216) [0xffffff80004fcf20] 7
 *1  call_continuation + 23 (kernel + 2098423) [0xffffff80004004f7] 8
   *1  vm_pageout_iothread_internal_continue + 1129 (kernel + 3134345) [0xffffff80004fd389] 8
     *1  vm_pageout_compress_page + 268 (kernel + 3135052) [0xffffff80004fd64c] 8
       *1  vm_compressor_pager_put + 127 (kernel + 2903871) [0xffffff80004c4f3f] (running) 8
 *2  vm_pageout_iothread_internal_continue + 0 (kernel + 3133216) [0xffffff80004fcf20] 9-10

  Thread 0x1ad              Thread name "VM_compressor"                         10 samples (1-10)         priority 91 (base 91)     cpu time 0.144s
  <IO tier 0>
 *3  call_continuation + 23 (kernel + 2098423) [0xffffff80004004f7] 1-3
   *3  vm_pageout_iothread_internal_continue + 1129 (kernel + 3134345) [0xffffff80004fd389] 1-3
     *3  vm_pageout_compress_page + 268 (kernel + 3135052) [0xffffff80004fd64c] 1-3
       *3  vm_compressor_pager_put + 160 (kernel + 2903904) [0xffffff80004c4f60] 1-3
         *3  vm_compressor_put + 1389 (kernel + 2900045) [0xffffff80004c404d] 1-3
           *1  WKdm_compress_new + 280 (kernel + 2092504) [0xffffff80003fedd8] (running) 1
           *1  WKdm_compress_new + 286 (kernel + 2092510) [0xffffff80003fedde] (running) 2
           *1  WKdm_compress_new + 309 (kernel + 2092533) [0xffffff80003fedf5] (running) 3
 *1  vm_pageout_iothread_internal_continue + 0 (kernel + 3133216) [0xffffff80004fcf20] 4
 *2  call_continuation + 23 (kernel + 2098423) [0xffffff80004004f7] 5-6
   *1  vm_pageout_iothread_internal_continue + 1187 (kernel + 3134403) [0xffffff80004fd3c3] 5
     *1  vm_page_free_list + 124 (kernel + 3219628) [0xffffff80005120ac] 5
       *1  vm_page_free_prepare_object + 146 (kernel + 3207826) [0xffffff800050f292] (running) 5
   *1  vm_pageout_iothread_internal_continue + 1129 (kernel + 3134345) [0xffffff80004fd389] 6
     *1  vm_pageout_compress_page + 304 (kernel + 3135088) [0xffffff80004fd670] (running) 6
 *4  vm_pageout_iothread_internal_continue + 0 (kernel + 3133216) [0xffffff80004fcf20] 7-10

Solution 1:

As for the workaround, restarting/killing WindowServer process temporarily helped:

# sudo pkill -HUP -u _windowserver

However, this will kill all processes, logs you out and re-open all windows again, so use it only when your system is in a very bad shape. However, it cleaned/removed all the swapfiles without the restart (at least for some time) till it gets out of the control again.


To monitor, what's creating/updating these swap files, run:

$ sudo fs_usage | grep swapfile

Or for page ins/outs, run:

$ sudo fs_usage | grep PAGE_

To see what WindowServer process is doing exactly, run:

$ sudo spindump -reveal $(pgrep WindowServer)

or for kernel_task, run:

$ sudo spindump -reveal 0