如何手动释放交换空间而不重新启动?


17

我在配备16GB RAM和SSD的MacBook Pro上使用High Sierra。

得到了15G的交换文件,是否有任何方法可以释放交换空间/文件而无需重新启动?

$ 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

我想以某种方式删除这些文件而不禁用它dynamic_pager


我试图:

  • 运行sudo purge(强制清除磁盘缓存);
  • 退出所有我不使用的应用程序;
  • 通过在创建虚拟交换文件之前创建它们来欺骗系统:

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

    但是它不起作用,因为尽管有000权限,系统仍然会创建/更改文件,这很奇怪。

由于某种原因,htop虚拟内存显示为532G,但我相信这是一个错误,因为top使用较少。

htop与top,PID用户主要虚拟资源CPU%MEM%TIME +命令,交换,内存,CPU,macOS

htop/ 上方top仅显示2个正在运行的任务,445个正在睡眠,CPU使用率不高,但是内核正忙于15G文件之间的交换。

我可以重新启动它,但是几天后这个问题每周都会重复。理想情况下,我想杀死并销毁(删除)交换文件,然后在同一运行中重新开始,但是macOS不允许我这样做。

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

是否有可用于强制dynamic_pager删除这些文件的黑客?我不需要那么多。

顺便说一句。这些交换文件为空!看到:

$ 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

如果这些为空,为什么macOS无法将其删除。我非常确定我的系统运行缓慢(例如平均加载和经常冻结鼠标)是由于这些文件的不断创建和删除引起的,因为我没有其他进程在大量使用我的CPU。

例如,写这篇文章之前,我有交换文件13G(1G每个),我的写作过程中,并粘贴上面的输出我结束了15G(这样做几乎没有),然后MacOS的实际删除swapfile14swapfile15(这样交换的13G)时写这个。然后swapfile13在写这句话时删除。然后swapfile16在写此句子时最多创建(16G)。并swapfile16在写这句话时再次删除。等等。真是一团糟。如果这些是空文件,那么当macOS没有在其中填充任何内容时,为什么macOS会继续创建它们。


其他资讯:

$ 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

这是一秒钟的WindowServer过程示例,其中至少半秒钟用于交换:

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

您只在顶部显示您的任务,其他用户呢。活动监视器还显示什么?
user151019

@kenorb找到答案了吗?高交换是macOS需要重启的唯一原因。将此问题提高100倍。
韦德·威廉姆斯

Answers:


7

至于解决方法,重新启动/杀死WindowServer进程暂时有帮助:

# sudo pkill -HUP -u _windowserver

但是,这将杀死所有进程,使您注销并再次打开所有窗口,因此仅在系统状况非常差时使用它。但是,它清除/删除了所有交换文件,而没有重新启动(至少一段时间),直到再次脱离控制。


要监视,正在创建/更新这些交换文件的内容,请运行:

$ sudo fs_usage | grep swapfile

或对于页面输入/输出,运行:

$ sudo fs_usage | grep PAGE_

要查看WindowServer确切执行的过程,请运行:

$ sudo spindump -reveal $(pgrep WindowServer)

或者kernel_task,运行:

$ sudo spindump -reveal 0

1
杀死WindowServer对我不起作用。交换是600GB,并在杀死Window Server之后减少到150GB。不幸的是,唯一使交换清零的是重新启动。
DuckDucking
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.