Swap is used even though >50% of RAM is still free

tokanizar

macrumors newbie
Original poster
Jul 21, 2011
20
3
New (ish) MBP 16" with 64G of RAM. I have never seen my RAM usage goes pass 50%, but have noticed the computer started using swap sometimes, about 3-4GB. Anyone knows why?

I kinda notice that this only happens when I have the laptop unplugged, and the swap will not go back to 0 after pluggin in. Not 100% sure, though.

2020-02-04 at 12.06 PM.png
 

barbu

macrumors 6502a
Jul 8, 2013
512
560
wpg.mb.ca
The best thing to with memory is ignore it. People go crazy when the RAM graph 'fills up' and the like...
Virtual memory, is allocated to every process that runs. Now, I know and love MacOS but I am much more familiar with Linux virtual memory mgmt, and I assume it is somewhat similar on the Mac. Linux's virtual memory (and yes, I know it's not exactly the same as swap) supports up to 64GB of virtual memory *per process*. Obviously that is over-subscribed since a low-end linux box might only have 2-4 GB of physical memory. But it shows on the resource monitor because that memory is mapped somewhere, usually on disk. It may never be needed or touched, but it is allocated, and if needed, the system will start swapping to those allocated addresses.

Anyway, instead of deep-diving modern memory management, let it suffice to say that your memory situation if completely normal and utterly not worth worrying about. MacOS will tell you when it's running out of memory and prompt you to close programs.

If you want to monitor which way your memory is trending, just watch the memory pressure indicator. Yours is extremely low, as your system is pretty much idling.
 

gilby101

macrumors regular
Mar 17, 2010
230
90
Tasmania
Linux's virtual memory (and yes, I know it's not exactly the same as swap) supports up to 64GB of virtual memory *per process*
Are you sure? You may be confusing 32-bit Linux with 64-bit versions. macOS (which is 64-bit) supports much more than 64GB. Linux uses some of the same memory management principles as Unixes, but the common heritage of Linux and macOS is in the distant past.

but have noticed the computer started using swap sometimes, about 3-4GB. Anyone knows why?
The swap usage (along with significant compressed) suggests that at some point since you last booted you were using lots of RAM.

the swap will not go back to 0 after pluggin in.
That is expected. Once some memory has been put into the swapfile it will not be recovered until either it is needed again by the process/app that owns it, or the process/app terminates. But it will only go back to zero after a reboot.

In simple terms, when macOS becomes short of memory (free memory is low) it will first try to compress some inactive memory and, if that is not sufficient, move memory (page-out) into swapfiles.

Good advice is not to over think memory unless you have a problem.

Using iStatMenus to follow memory usage:
Use the history graphs to pinpoint when compressed memory and swapfile usage increased.
 
Last edited:

throAU

macrumors 603
Feb 13, 2012
5,563
2,627
Perth, Western Australia
This is normal.

A modern sensible OS will pro-actively put idle stuff in swap so that IF you make a large demand on memory (e.g., you have 4 GB of total 16 GB free, but say 4 GB is IDLE, and suddenly request 8 GB), it does not need to start swapping it out at that point - it can just immediately make the memory available as it was already swapped out whilst idle. Otherwise you'd be waiting for 4 GB to be swapped out at that point before the memory could be allocated. That's work the OS can do in advance, if the processes are IDLE. So it does.


Even better, if idle memory is swapped to disk, it can be used for disk cache. So, see all that swap? Look at your "cached files" (or on your older platform 'cache') number. Guess what is very likely in there? That's right - the stuff (or at least much of it) that your PC swapped out.

So, chances are it may well still be in RAM (in the form of disk cache) anyway. But in a form that can be INSTANTLY dropped and re-allocated if needed.

This is why Apple introduced a "memory pressure" graph. Because looking at the individual numbers is a bit more complicated than it may appear.

The big take-away for the past decade or two with regards to memory management (in pretty much every modern platform) is this:

Stop trying to out-smart it.

As above, ignore it until performance becomes a problem, THEN go trying to diagnose the issue (and start with the "memory pressure" reading). Don't go trying to diagnose issues that do not exist.
 
Last edited:

konqerror

macrumors 68000
Dec 31, 2013
1,516
2,498
In simple terms, when macOS becomes short of memory (free memory is low) it will first try to compress some inactive memory and, if that is not sufficient, move memory (page-out) into swapfiles.
macOS carries over behavior from BSD. It will preemptively page-out data that is not used, even when RAM is sufficient, to avoid having to page-out when demand suddenly increases. That's the behavior OP is seeing.

It made sense back in the day when computers had little RAM and disk paging was frequent, but Apple has never eliminated the behavior.
 

throAU

macrumors 603
Feb 13, 2012
5,563
2,627
Perth, Western Australia
It made sense back in the day when computers had little RAM and disk paging was frequent, but Apple has never eliminated the behavior.
It still makes sense today when you have very fast IO which makes swapping fast (check - most machines are high speed SSD) and limited RAM (check - most apple machines sold are extremely stingy on memory).

There is zero point carrying stuff in RAM that is idle, at least swap to disk and keep it in cache, as per my post above.
 
  • Like
Reactions: tokanizar

konqerror

macrumors 68000
Dec 31, 2013
1,516
2,498
It still makes sense today when you have very fast IO which makes swapping fast (check - most machines are high speed SSD) and limited RAM (check - most apple machines sold are extremely stingy on memory).
I think the concerns are that writing to disk costs power, not only to wake and run the drive, but now you have pay to encrypt and decrypt swap for security reasons. Second, you can get a pretty significant amount of flash wear from doing that. Both of these weren't on the radar in the 1980s.
 
  • Like
Reactions: tokanizar

gilby101

macrumors regular
Mar 17, 2010
230
90
Tasmania
A modern sensible OS will pro-actively put idle stuff in swap
I absolutely agree, but on that basis macOS is not sensible. The 'compressor' (part of the macOS kernel) is called whenever free memory is low and it first compresses inactive RAM and only if that is not sufficient does it write memory into swapfiles. Unixes have always been reluctant to use swapfiles/pagefiles unless really necessary. This in contrast to the VAX/VMS family which do pro-actively write inactive memory into pagefiles.
 
Last edited:
  • Like
Reactions: tokanizar

throAU

macrumors 603
Feb 13, 2012
5,563
2,627
Perth, Western Australia
I think the concerns are that writing to disk costs power, not only to wake and run the drive, but now you have pay to encrypt and decrypt swap for security reasons. Second, you can get a pretty significant amount of flash wear from doing that. Both of these weren't on the radar in the 1980s.
Reading from SSD also costs power - and if you have IDLE stuff in swap, then you can use more RAM for disk cache (to avoid those reads).

The amount of wear this will cause is not enough to be a concern, the typical user will likely see a bunch of idle stuff swapped to disk once per boot (e.g., in my case 1-2 GB on my 8 GB macbook), and even if i was to boot 3 times per day, that's what... 6 gb per day? Even if we scale that by a factor of 10 - so 60 GB per day.... at that rate my SSD would take many YEARS to see significant wear from that (even some SSDs from 5 years ago could handle > 1 PB (e.g., 1 million GB) writes on a 250 GB drive before starting to re-allocate sectors). That's based on real-world testing by the way, not vendor specs.


It took up to 18 months of continuous 100% writes to kill those drives.
 
Last edited:
  • Like
Reactions: tokanizar

tokanizar

macrumors newbie
Original poster
Jul 21, 2011
20
3
Thanks guys for very detailed answers! It's much more clear to me now that I don't have to worry at all, because
1. It's normal behavior of the OS. Sensible or not, that's another argument, but I have faith that the people who made the OS are (significantly) smarter than I am 😄
2. Swap use on SSD shouldn't be an issue, regarding endurance of the flash drives.

Using iStatMenus to follow memory usage:
Use the history graphs to pinpoint when compressed memory and swapfile usage increased.
Here we go. RAM usage stays below 50% all the time (some might say I'm not using my computer properly) and swap kicked in a few times. One of those times was 5am, when I was sleeping. ¯\_(ツ)_/¯

RAM
2020-02-11 at 9.47 AM.png


SWAP
2020-02-11 at 9.47 AM.png
 
  • Like
Reactions: throAU

Fishrrman

macrumors P6
Feb 20, 2009
18,241
6,094
For years now, I've DISABLED VM disk swapping with the following terminal command:
sudo launchctl unload -wF /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
(reboot is necessary).

Then, check with this command:
sysctl vm.swapusage

If VM is off, report should be:
vm.swapusage: total = 0.00M used = 0.00M free = 0.00M

My Mac runs great and doesn't crash.
I have 16gb of RAM in my 2018 Mini, but this worked fine with my 2012 Mini that had 10gb of RAM as well.
Of course, I'm mindful of how many apps and files I have open, and close unneeded apps when I'm not using them.
 
  • Like
Reactions: tokanizar

gilby101

macrumors regular
Mar 17, 2010
230
90
Tasmania
Here we go. RAM usage stays below 50% all the time
I have just realised something in your graphs (I should have seen it right at the beginning). There is a memory setting in iStatMenus called "Hide inactive memory". I think you have this enabled. You should untick it.

With the option enabled, iStatMenus is hiding "inactive RAM" and adding it into "free memory". I have it disabled. Look at this from my 40GB RAM iMac which shows 50% to 60% used (wired + active + compressed):

Screenshot 2020-02-12 at 8.17.20 am.png


Screenshot 2020-02-12 at 8.40.19 am copy.png

The light grey is the 'inactive memory'. Even though I have lots of compressed RAM and some swap usage, my free+inactive is nearly 50% like yours. This is normal and the more RAM you have the more is likely to be 'inactive'.

The inactive RAM is a cache of memory belonging to processes on your system, but which have not been used for some long time. macOS keeps such memory in RAM because it is (perhaps) likely to be used again in the future. The algorithm deciding which RAM to keep as inactive, or compressed, or in the swap file is complex (and not published except for what is in the open source parts of the kernel).

My swapfile usage graph has the same characteristic as yours. Firstly after a reboot, the compressed RAM (purple) and swapfile are zero. At various times I do something which requires more RAM (or maybe I just have so many apps running), the really 'free' memory becomes low and macOS compresses some of the inactive (grey goes to purple) and often also sends some of it into the swap file. I only have 40GB RAM (and run lots of apps at once) so I see bursts in compressed and swap more frequently than you with 64GB.

For most people the memory pressure is the only thing they need to worry about. It is a nice single value which everyone can comprehend on some level. Once you start going deeper than that you are in for a steep learning curve (or incomprehension!!). Like some others in this thread, I find OS internals interesting so have climbed that curve to a certain extent.

I hope that helps.

ps. you are using your computer properly and, at times, using all the RAM.
 
  • Like
Reactions: tokanizar

sdf

macrumors 6502
Jan 29, 2004
292
236
New (ish) MBP 16" with 64G of RAM. I have never seen my RAM usage goes pass 50%, but have noticed the computer started using swap sometimes, about 3-4GB. Anyone knows why?
I'm not meaning to be insulting here, but I want to explain this simply so everyone gets it:
  1. Disk is a resource.
  2. Memory is a resource.
  3. Memory is faster and consumes less power than disk.
  4. Any time the system can load something into memory for expected reuse it should.
Let's take a simple example. The OS wants a directory table, so it's powering up the hard drive (or SSD). It wants to grab, say, 32k. It's likely it'll want another 32k after that. So what's the energy cost for grabbing both the current request and the next at once? If there's not much energy cost, instead of getting 32k, why not get 32MB and keep it held lightly so that some other request purges it out? That saves spinning up the hardware, it means more performance, and memory might as well be used if it's there.

Don't worry about memory use. On a modern computer, the OS should be using lots of memory. Don't even worry about "low" memory. Worry about running out to the point it compromises performance.
 

throAU

macrumors 603
Feb 13, 2012
5,563
2,627
Perth, Western Australia
My Mac runs great and doesn't crash.
My mac runs great and doesn't crash. I haven't tried to mess with OS level features that are implemented the way they are by design.
- - Post merged: - -

Don't worry about memory use. On a modern computer, the OS should be using lots of memory. Don't even worry about "low" memory. Worry about running out to the point it compromises performance.
That.

OS kernel developers (many with science degrees) have been tweaking and improving this stuff for 30-50+ years at this point. I'm sorry but no, Mr End User reading activity monitor graphs and trying to out-smart the kernel's memory manager is not going to do a better job of it.

Sure, if you have 16 GB of RAM and can guarantee you never need to use more than 16 GB of RAM then maybe you can avoid swap.

At what cost? If you run an application with a memory leak you will perhaps have a hard out of memory crash. You will not be able to open files larger than a certain size, etc.
 
Last edited:
  • Like
Reactions: barbu and tokanizar

tokanizar

macrumors newbie
Original poster
Jul 21, 2011
20
3
The light grey is the 'inactive memory'. Even though I have lots of compressed RAM and some swap usage, my free+inactive is nearly 50% like yours. This is normal and the more RAM you have the more is likely to be 'inactive'.

The inactive RAM is a cache of memory belonging to processes on your system, but which have not been used for some long time. macOS keeps such memory in RAM because it is (perhaps) likely to be used again in the future. The algorithm deciding which RAM to keep as inactive, or compressed, or in the swap file is complex (and not published except for what is in the open source parts of the kernel).

My swapfile usage graph has the same characteristic as yours. Firstly after a reboot, the compressed RAM (purple) and swapfile are zero. At various times I do something which requires more RAM (or maybe I just have so many apps running), the really 'free' memory becomes low and macOS compresses some of the inactive (grey goes to purple) and often also sends some of it into the swap file. I only have 40GB RAM (and run lots of apps at once) so I see bursts in compressed and swap more frequently than you with 64GB.

For most people the memory pressure is the only thing they need to worry about. It is a nice single value which everyone can comprehend on some level. Once you start going deeper than that you are in for a steep learning curve (or incomprehension!!). Like some others in this thread, I find OS internals interesting so have climbed that curve to a certain extent.

I hope that helps.

ps. you are using your computer properly and, at times, using all the RAM.
You are right, sir, and this is the updated graph. Still no sign of all RAM being used, so ¯\_(ツ)_/¯. However I'm not worried anymore after your, and others', explanation! Thank you!

2020-02-11 at 7.42 PM.png


I'm not meaning to be insulting here, but I want to explain this simply so everyone gets it:
  1. Disk is a resource.
  2. Memory is a resource.
  3. Memory is faster and consumes less power than disk.
  4. Any time the system can load something into memory for expected reuse it should.
Let's take a simple example. The OS wants a directory table, so it's powering up the hard drive (or SSD). It wants to grab, say, 32k. It's likely it'll want another 32k after that. So what's the energy cost for grabbing both the current request and the next at once? If there's not much energy cost, instead of getting 32k, why not get 32MB and keep it held lightly so that some other request purges it out? That saves spinning up the hardware, it means more performance, and memory might as well be used if it's there.

Don't worry about memory use. On a modern computer, the OS should be using lots of memory. Don't even worry about "low" memory. Worry about running out to the point it compromises performance.
Thanks for your response! To be clear, I was not worried about "memory" being used here (if we are talking about the same term). I was just worried something was wrong with my mac, hardware wise, or software wise (more likely because I think Catalina is buggy as ****), so that it tries to use swap unnecessarily. It was from my (wrong) impression that when RAM is still available, swap should not be ultilized at all. I was wrong, apparently!
 

topcat001

macrumors regular
Nov 17, 2019
144
60
I'll add one more comment, besides generally agreeing that the kernel is doing the "right thing":

OP, since you mentioned that swap tends to get used if you leave the computer unplugged, my guess would be it's going through sleep/wakeup sessions (among other things). On my MBP 16 (and other Macs) I have noticed that it writes to swap before sleeping. This is a feature which allows it to recover from sleep even if power fails, because just like hibernation, the memory contents are saved to swap.
 

barbu

macrumors 6502a
Jul 8, 2013
512
560
wpg.mb.ca
Are you sure? You may be confusing 32-bit Linux with 64-bit versions. macOS (which is 64-bit) supports much more than 64GB. Linux uses some of the same memory management principles as Unixes, but the common heritage of Linux and macOS is in the distant past.
64 GB per process, not total memory physically present. There may be hundreds of processes running. All OSes oversubscribe memory like this, to my knowledge.
 

topcat001

macrumors regular
Nov 17, 2019
144
60
Not all. In UNIX this is indeed common practice, but Windows will fail the allocation if the total for all processes exceeds RAM + max swap. Interestingly in Linux this can be configured using a sysctl (not the default behaviour).
 
  • Like
Reactions: tokanizar