6

As disabling swap and/or compressing memory is not recommended and also not a stable option after 10.9 Mavericks (despite the vm setting exists), I'm doomed after few days of running my Mac with my memory writing to the far-from-optimal pages of memory. As everything gets to swap or compressed memory so easily, I need to reboot my system relatively often (despite my 16GB RAM).

I'm searching for a solution which saves me from these slowdowns.

For example in Linux zramswap is optional. Also Linux has its swappiness value between 0 and 100 variable like

vm.swappiness=5

Also I may consider a solution about file cache (which usually randomly eats up tons of memory for no valid reason and fails to drop it before RAM turns to the less optimal purgatories of compressed memory and swap). For example here ZFS has an option on FreeBSD to maximize the size of file cache in memory:

vfs.zfs.arc_max="1536M"

In macOS the best known workaround for the file cache issue is running

# /usr/sbin/purge

Which is even "cronnable". So this flushes file cache, but unlikely to be optimal. It flushes too many things. Also if something is already in swap and/or compressed memory, despite the purge it stays there, so those softwares using them stay slow (and I feel that slowness, trust me).

Is there any solution to make macOS less likely to use file cache, compressed memory or swap (but still keep the first for performance and the latter two for emergency)?

  • 3
    The trouble with the proviso "when really needed" is all the brains at Cupertino have already decided when that should be. What you really mean is "when I think it ought to" which is a totally different thing. – Tetsujin Oct 22 '18 at 17:08
  • On Linux I control it myself and it works much much better. There is swap and only used in emergency. It's not as simple as "when I think it should". It's more like "when I experienced it should" and "when I experienced it shouldn't". The way it cannot be fine tuned is a huge disadvantage of macOS. – dszakal Oct 23 '18 at 12:39
  • 1
    Okay, then tell me how do you add more RAM to a pre2018 Macbook Pro 15 or any other Macbook (Pro, Air, etc.). The 16GB is the maximum since around 2010 in most of them (except 2018 Macbook Pro 15 inch). And also the OS decides to swap and compress when the ram is not even full. (Same with Linux unless you lower swappiness value, but there at least you can). Please stop trying to convince me that Apple made here good decisions related to memory management as they didn't, and also please stop suggesting impossible solutions working for almost no one practically. – dszakal Oct 23 '18 at 12:44
  • Are you sure the problem doesn't stem from the applications themselves? – adib Nov 5 '18 at 15:04
  • Yes I am sure :) – dszakal Nov 8 '18 at 16:37
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

  • Disabling swap via vm.compressor_mode worked up to 10.9, but froze 10.10 and later OS X/macOS versions. :( – dszakal Oct 22 '18 at 17:06
  • @dszakal The compressor_mode settings were only introduced along with vm–compression 10.9 and the settings are present in 10.12 as well. I can't boot-test the 10.14 machine now but the setting is in 10.14 as well… What is not working with this in your setup? – LangLangC Oct 23 '18 at 12:54
  • on 10.10 it froze after an hour and on 10.11 it froze when you pressed the shutdown button. So I didn't dare to try after 10.12 as noone dared to answer this: apple.stackexchange.com/questions/275303/… – dszakal Oct 23 '18 at 13:09
3

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.

  • When you talk also about Linux virtualised it becomes quite confusing to decode when you write VM: about VirtualMachine or VirtualMemory that "needs" tweaking. (I find that memory management in macOS seems sometimes quite crazy, and usually better managed in Linux, with quite a few nut than equally egregious exceptions; but how is adding a Linux-Vmachine with its own Vmemory management of benefit (using even more memory by design of that setup), and how do you fix memory management/leaks of all that closed software you might depend on? Except for filing bu reports that is.) – LangLangC Nov 3 '18 at 16:29
  • Since we compare approaches: both macOS & Linux ship with algos and default settings that were determined to work for a certain subset of applications. These cannot cover all scenarios equally well and both have assumptions for a certain default. That means for Linux to run well on Laptops you really benefit from tweaks to the bigger iron defaults chosen. For macOS you mainly benefit from the few tweaks that you can enable to use it as bigger iron (server). AFAIK there are only very few options available on mac & the main point you correctly point out is that the gains are small. – LangLangC Nov 3 '18 at 16:37
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'.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .