Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tokanizar

macrumors newbie
Original poster
Jul 21, 2011
21
5
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 65816
Jul 8, 2013
1,263
1,052
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 68030
Mar 17, 2010
2,946
1,630
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 G3
Feb 13, 2012
9,198
7,342
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 68020
Dec 31, 2013
2,298
3,701
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 G3
Feb 13, 2012
9,198
7,342
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.
 

konqerror

macrumors 68020
Dec 31, 2013
2,298
3,701
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.
 

gilby101

macrumors 68030
Mar 17, 2010
2,946
1,630
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 G3
Feb 13, 2012
9,198
7,342
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
21
5
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: ikjadoon and throAU

Fishrrman

macrumors Penryn
Feb 20, 2009
29,233
13,303
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 68030
Mar 17, 2010
2,946
1,630
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.
 

sdf

macrumors 6502a
Jan 29, 2004
924
1,295
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 G3
Feb 13, 2012
9,198
7,342
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.
[automerge]1581468546[/automerge]
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:

tokanizar

macrumors newbie
Original poster
Jul 21, 2011
21
5
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 6502
Nov 17, 2019
287
141
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 65816
Jul 8, 2013
1,263
1,052
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 6502
Nov 17, 2019
287
141
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

meme1255

macrumors 6502a
Jul 15, 2012
748
598
Czechia
This is common, however INCORRECT behavior! I have filed several developer bug reports to Apple and I am waiting for further info. The disk should NOT be used if there is any free memory available.
 

iMacDragon

macrumors 68020
Oct 18, 2008
2,399
733
UK
This is common, however INCORRECT behavior! I have filed several developer bug reports to Apple and I am waiting for further info. The disk should NOT be used if there is any free memory available.

I can definitely see some purpose to writing out idle pages to swap when memory starts getting near full, but not yet full, so that a larger allocation can be performed fast without waiting for swap out. Hard to say at what point this should happen though.
 

sdf

macrumors 6502a
Jan 29, 2004
924
1,295
This is common, however INCORRECT behavior! I have filed several developer bug reports to Apple and I am waiting for further info. The disk should NOT be used if there is any free memory available.

That would be silly behaviour. In an ideal world, memory would be treated entirely as cache over disk. Having available RAM used to the point where it's not possible to allocate is something Apple's engineers are too smart to allow. For that matter, RAM marked as read only might as well be written out when the disk is in use rather than leaving ti to a nebulous and possibly critical "later." Writing the memory out when demand is low lets the memory be actually freed when demand is high faster than waiting until demand is high to write out the memory to disk.

They've done the math.
[automerge]1585352961[/automerge]
I can definitely see some purpose to writing out idle pages to swap when memory starts getting near full, but not yet full, so that a larger allocation can be performed fast without waiting for swap out. Hard to say at what point this should happen though.

My vote goes to "as soon as there's no performance cost," but Apple's engineers have done the math. I haven't.
 

auxbuss

macrumors 6502
Feb 18, 2014
453
329
UK
This is common, however INCORRECT behavior! I have filed several developer bug reports to Apple and I am waiting for further info. The disk should NOT be used if there is any free memory available.
Yeah, that's not how swap works.

Here's a good article – re Linux, but the same principles apply to macOS – written by someone who works on Linux memory management: In defence of swap: common misconceptions
 

meme1255

macrumors 6502a
Jul 15, 2012
748
598
Czechia
Yeah, that's not how swap works.

Here's a good article – re Linux, but the same principles apply to macOS – written by someone who works on Linux memory management: In defence of swap: common misconceptions
Unfortunately the macOS swap doesn't work the same way as the Linux one (that one is OK).



You can easily test this for yourself. Shoot i.e. 64 GB worth of images with iPhone/whatever, and start to import in Bridge or whatever. Keep an eye on the activity monitor while importing, and note the import speed.

Cache will build up continuosly, as the OS thinks it is wise to hold on to the RAW files in RAM. Once total memory hits installed RAM, the OS will start to ”compress” and swap, and you will see the import speed crawl to a halt. The OS will keep on prioritizing cache, and use more and more swap to keep it this way.

This is NOT normal. It is a design flaw.

Now start Photoshop and Illustrator to work with your images, and the situation will be absurd, as the OS will continue to retain the cache and shrink the available RAM for applications and documents further. All that RAM you installed is used to keep useless files in memory.

If swap is used, your RAM is too low. But even if you have a fair amount of RAM, Mac OS will not use it efficiently, but prefer to swap rather than get rid of stale cache. This never ever happens in Linux, which will ditch cache before using swap.
 
  • Like
Reactions: pdoherty

throAU

macrumors G3
Feb 13, 2012
9,198
7,342
Perth, Western Australia
Do you guys recommend disabling swap memory then? I also have 64GB of RAM on my MBP 16 and I don't understand why the OS is using swap memory already when I have more than 50% of ram available too.

Disabling Swap Memory and why you should

No.

Swap is there for a reason; as above swapping things to SSD pro-actively means the machine can respond faster if a large memory allocation comes in.

It's swapping IDLE things to memory before it has to so that if/when it does get short of memory the idle non-changing portions of ram are already swapped out. All it has to do is de-alloc the real memory. If it waited until there was memory pressure before starting to swap, there would be a delay while you waited for things to page out.

Leave it to do its thing.

For 90% plus of people, trying to turn off swap or using memory cleaners, etc. is fighting against what the OS is doing to improve responsiveness and performance.

Unless you KNOW you have a performance problem that you can then objectively test with swap on vs. swap off, leave it be.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.