Tell macOS to only use swap and compressed memory when really needed (macOS 10.14 Mojave)
Solution 1:
I have the same issue and completely sympathise. Unnecessary swapping in the face of other options (e.g. shrinking the cached files area) has been a problem for years. It's made even worse under Mojave, where free memory - not even disk cache, but completely free - is present, but the system decides to use swap anyway. On an SSD, every write causes wear & tear to the hardware, so this is actually (albeit in a small way) physically damaging behaviour. Since the SSDs in modern Macs are soldered onto the main board, the situation is dire.
I've been told many times by very confident posters on sites such as this that free memory is a bad idea, because it's sitting there consuming power and being of no other use. Disc cache (at least) should consume it. I agree with this; Mojave does not, compressing data and swapping it to disc even when gigabytes of free memory are present. On my home laptop, sleep cycles make it even worse. Right now, after a wake-up the machine has 11GB free, 5GB in use and 6GB swapped. The gibberish "memory pressure" colour is green, whatever that means. It's utterly absurd and indefensibly broken.
I used to think there must be something I was installing or doing wrong, but on brand new Macs at work with nothing extra installed, consuming RAM via opening lots of tabs in Safari will soon enough show use of swap space despite lots of free RAM.
And yes, it lags - badly at times. This definitely has a performance impact.
I know of no solution. Apple Engineering claim it 'works as designed'.
Solution 2:
I say this with kindness, but no, no, no.
Don’t change the fundamental design and process of virtual memory management of macOS because you have a system or workload that leaks so badly that you have accumulating swap piling up and the algorithm breaks down. Any change you make to the algorithm will still break if you have software that’s not allocating memory correctly, so this problem probably needs to return to isolate and identify what is causing the memory pressure so that compression and swap become a symptom of the underlying issue.
Now - you might have a super good reason why you’re asking to alter the virtual memory, but macOS micro kernel isn’t designed for the level of tweaking that Linux is and if you truly need to manage VM that tightly, I would encourage you to either locate that code on a separate server via putting it in the cloud, on a second local device or even virtualizing Linux guest OS on top of macOS.
Then you’ll have the best memory turning for the actual OS and the virtual software (VMWare Fusion is my general first recommendation, but check out Parallels or even some free options if you have time and inclination) so you can put the code that needs an iron fist governing virtual memory on Linux and let the mac apps be mac apps.
And just to go out on a limb, in the seventeen years since OS X shipped, I’ve worked with dozens of people that went down the path you propose - find a case where virtual memory signals a problem and then change VM to overcome those software needs, in every case, we ended up fixing the applications or fixing where we run things instead of finding a magic setting or mortification that didn’t cause more harm than good.
I do encourage everyone to try these things - especially if you have desire to know the internals of an operating system, but the premise here that VM needs to change doesn’t seem to ring true based on the details provided or the long experience of attempting this in the past. Please let us know if you find a setting that works for you and don’t down vote other people if they suggest things - this is how we all learn by hashing out engineering and software challenges - even against odds of it not working in the end.
Solution 3:
First and best line of defence would be to kill processes or services not needed.
Then you might control the vm compression:
$ sysctl -a vm.compressor_mode
vm.compressor_mode: 4
Set these variables via an nvram command:
Mode 0x1, VM_PAGER_DEFAULT, turns off memory compressor and swapping, which is proved to be harmful to the system stability. Modes 0x8, 0x10 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 0x2, VM_PAGER_COMPRESSOR_NO_SWAP, is the best choice here. It provides memory compressor with swapping disabled. 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.
Avoid memory being compressed
Although memory compression is fast and is designed to relieve memory pressure, the best performance can only be achieved when none of the memory is being compressed. Use Activity Monitor or the following command to keep an eye on memory usage:$ top -o CMPRS
From my experience, macOS starts to compress memory when the memory utilization is close to 80%. Try limiting the number of running apps to a low number, and restart or kill apps that consume excessive memory. Then your Mac should run as fast as it should. Use Activity Monitor or the following command to see which apps are using most of the memory:
$ top -o MEM
via medium
There are more settings to play with and accessible in similar ways but they all seem not really worth it