MacOS uses virtual-memory / swap a lot
I have a MacBook Pro at work and I think it uses too much swap instead of RAM.
Model Name: MacBook Pro
Model Identifier: MacBookPro13,1
Processor Name: Intel Core i5
Processor Speed: 2 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 16 GB
Boot ROM Version: MBP131.0205.B22
SMC Version (system): 2.36f97
Serial Number (system): C02TL0KGGVC8
Hardware UUID: 09325653-7FB0-52CC-A599-063539D1010A
I am used to monitoring UNIX system activity using htop
. I notice that my OS X never uses more than half of the RAM, but uses more swap instead. Usually 7 GB of swap, but it can be more.
My question is, should not my OS use more RAM before swap ? I read about how OS X handles the swap and I feel like it is a loss of computational power. Writing pages in and out is really time consuming and is not as fast as using RAM
I try to compare with my Ubuntu 16.04 distribution at home which doesn't swap unless the memory is full. But maybe the 2 OS have different behaviors, though they are both UNIX based.
I also printed out the output of the vm_stat
command to check if there were any difference, because maybe htop
is not reporting it accurately. I noticed that htop
reports a varying maximum swap capacity, from 3 to 16. Why is it not a fixed size partition as in Ubuntu?
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 6743.
Pages active: 1005803.
Pages inactive: 903490.
Pages speculative: 361.
Pages throttled: 0.
Pages wired down: 837598.
Pages purgeable: 9241.
"Translation faults": 6115698117.
Pages copy-on-write: 107293117.
Pages zero filled: 2540298644.
Pages reactivated: 1039487337.
Pages purged: 52550607.
File-backed pages: 482299.
Anonymous pages: 1427355.
Pages stored in compressor: 5968822.
Pages occupied by compressor: 1439832.
Decompressions: 1456775259.
Compressions: 1606172332.
Pageins: 1393246141.
Pageouts: 25235109.
Swapins: 562867577.
Swapouts: 582845342.
Solution 1:
First - yes, the OS in general will use RAM before swap. There's no loss of computational power involved in that.
You seem to have jumped to the conclusion that macOS is using swap, when it could as well be using free memory based on the htop screenshot. You cannot make that conclusion from a simple sample of the memory usage.
In reality, you could have been running programs that were using a lot of RAM forcing macOS to swap memory to disk. Then you quit some of those programs (or they deallocated a chunk of RAM). Then if you take a htop sample now, you'll see that RAM usage is low (lots of free RAM), but you're also using lots of swap.
But that is not a problem - in fact it is intended to be this way! It wouldn't be wise for macOS to start swapping in those pages that have been placed on disk. It will do this when they're required (i.e. used by programs), but until that happens, it could be that those pages would never be used - and thus time spent swapping them in will be lost.
You write that in comparison to your Ubuntu PC you observe a different behaviour. You should know that you can actually change that behaviour. On Linux you can use sysctl to set the vm.swappiness variable. It will change how aggressive the OS is in swapping things out to disk.
You also ask why your macOS system does not use fixed size swap as Ubuntu does. In fact, Ubuntu also supports dynamic sized swap using the "swapspace" command. You can set minimum and maximum sizes, and it will dynamically add/remove swap space as needs change.