Friday, January 5, 2018

Squeeze every last drop out of macOS High Sierra

Disable swapping

macOS tends to swap memory to disk when there are still tons of free memory available. Swap, aka. virtual memory, is disk-based and thus it is terribly slow, even on a lightning fast SSD. Disabling swap is absolutely fine, as long as you keep the number of running applications low.
When swap is disabled, macOS will try to utilize as much free memory as possible and won’t complain at all until under extreme memory pressure. When it does under pressure, kernel panic will follow. So keep an eye on memory pressure and you will be fine.
There are a number of different modes that macOS manages its memory. In vm_pageout.h:



#define VM_PAGER_DEFAULT                        0x1  /* Use default pager. */
#define VM_PAGER_COMPRESSOR_NO_SWAP             0x2  /* In-core compressor only. */
#define VM_PAGER_COMPRESSOR_WITH_SWAP           0x4  /* In-core compressor + swap backend. */
#define VM_PAGER_FREEZER_DEFAULT                0x8  /* Freezer backed by default pager.*/
#define VM_PAGER_FREEZER_COMPRESSOR_NO_SWAP     0x10 /* Freezer backed by in-core compressor only i.e. frozen data remain in-core compressed.*/
#define VM_PAGER_FREEZER_COMPRESSOR_WITH_SWAP   0x20 /* Freezer backed by in-core compressor with swap support too.*/

By default, in-core compressor with swap backend, or VM_PAGER_COMPRESSOR_WITH_SWAP, is used. You can verify this by the command below:
$ sysctl -a vm.compressor_mode
vm.compressor_mode: 4
Mode 0x1VM_PAGER_DEFAULT, turns off memory compressor and swapping, which is proved to be harmful to the system stability. Modes 0x80x10 and 0x20 are the so-called “freezer” modes, which “freeze” the OS instantly when memory is under pressure. You don’t want to try them.
Mode 0x2VM_PAGER_COMPRESSOR_NO_SWAP, is the best choice here. It provides memory compressor with swapping disable. In other words, when memory is under pressure, macOS will try to compress the active but non-wired memory, thus freeing part of the memory back to the system. macOS uses WKdm algorithm to compress and decompress the memory, which is fast and battery-efficient. Yet, kernel panic is still possible if there is no more compressible memory.
To change from mode 0x4 to 0x2, use this command and reboot:
$ sudo nvram boot-args="vm_compressor=2"
When using mode 0x2, memory pressure must be monitored closely to avoid kernel panic. Once the compressed memory grows close to 50% of total memory capacity, you would either like to close some of the running applications, or simply reboot the system.

Disable System Integrity Protection (SIP)

You need to disable parts of SIP for the above to work while leaving others enabled.
If you run csrutil status, even while booted normally, you will see the component parts of it. Each of these can be selectively disabled by running one of the following commands while booted into Recovery mode:
  • csrutil enable --no-internal
  • csrutil enable --without kext
  • csrutil enable --without fs
  • csrutil enable --without debug
  • csrutil enable --without dtrace
  • csrutil enable --without nvram
You can disable two or more components by structuring the command as follows:
  • csrutil enable --without kext  --without debug