11

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.

htop report

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.
2
  • 1
    The machine's been up 15 days; you've pretty much no chance of chasing down what it's been doing over that time. Start on a fresh boot.
    – Tetsujin
    Commented Dec 19, 2017 at 11:27
  • Thank you for pointing out I rebooted and it cleaned my swap. I thought macOS better clean unused swap files when the process is killed (I bet my guilty process was jupyter notebook running huge dataset).
    – MCMZL
    Commented Dec 20, 2017 at 14:04

1 Answer 1

10

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.

10
  • " but until that happens, it could be that those pages would never be used " I guess that swap pages cannot be cleaned when a program is killed because pages are program agnostic ? This lead to lots of unused swap files over time, but does not necessarily diminish os performance with a dynamic size swap ?
    – MCMZL
    Commented Dec 20, 2017 at 14:09
  • Also I have mixed feelings about the htop figure for RAM capacity. It never goes over 9G but I think it only refers to the used memory (green) and not the cache memory () . But looking at the loading bar it seems that green + yellow reaches almost full capacity
    – MCMZL
    Commented Dec 20, 2017 at 14:15
  • 1
    No, you're mistaken. Swap pages can be cleaned when a process is killed - the OS knows exactly which pages are in use by which processes. In practice the pages in the swap file are marked as non-used, and will periodically be reclaimed. Dynamic swap size does not mean that it only grows, it retracts again.
    – jksoegaard
    Commented Dec 20, 2017 at 16:14
  • Regarding the htop figure for RAM capacity. You seem to have misunderstood that it is actually showing two seperate things. The text shows (usedmem - buffers - caches) compared to total RAM, whereas the graphical bar shows the usedmem, buffers and caches as three seperate colors summing up to 100%. So unless you have exactly 0 byte buffers and 0 byte cache, the ratio of both won't match up.
    – jksoegaard
    Commented Dec 20, 2017 at 16:20
  • okey I knew it was not what I expected. By 'text' you refer to the figures , here 9.88/16GB on the screenshot ?
    – MCMZL
    Commented Dec 20, 2017 at 16:58

You must log in to answer this question.

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