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

Analog Kid

macrumors G3
Mar 4, 2003
9,362
12,612
it kind of drives me crazy that sudo purge only cleans cached files and not swap
I really don't understand why this would bother anyone... Or why anyone would be manually running 'sudo purge' in the first place, except for in very specific cases. Are you planning to hot swap your boot drive on a running system or something?

How Chrome handles its memory management is really more about Chrome performance. Thank gods there aren't more system statistics exposed by Activity Monitor-- I feel like each one triggers some weird anxiety in the user community.
 
  • Like
Reactions: name99

Mity

macrumors 6502a
Original poster
Nov 1, 2014
798
728
This thread is now assuming Chrome is the culprit but I don't believe that to be true. As I stated, I have 50+ Chrome tabs open on my 16" Intel MBP and there has been zero swap use.
 

theorist9

macrumors 68040
May 28, 2015
3,883
3,067
There's also a command in Terminal: vm_stat

See man vm_stat for details.
If you're comfortable with the command line then this is pretty easy to check. There's a command called 'vm_stat' that tells you what the virtual memory system is doing.

If you type 'vm_stat 60' at the command line, the first line will tell you the summary since reboot and then once every 60 seconds it will print another line telling you what's happened in the last minute.

"swapouts" is the column you're interested in-- if that column is zero, then nothing has been written to disk. It's the number of 16kB pages written if you're worried about bytes.

Ignore the "pageouts" column, it's a different, more subtle, thing that's not interesting here because it's all within physical RAM, no disk access.

Every now and then, the command will reprint the column headers and the summary since boot, which is annoying, but I don't think it can be disabled so if you leave it running a long time and see big numbers periodically, it's the summary.

'vm_stat -c 11 60' will show you stats once a minute for 10 minutes. It seems to count the summary line in the "count" so you need to use a duration+1 for the count. 60 is obviously the number of seconds in this example, so you can set it to something shorter or longer depending on your interest.
I was thinking about suggesting vm_stat to the OP but thought that, for their purposes, just monitoring the Activity Monitor window might be easier. But of course the OP might want to explore that anyways.

I suppose the advantage of vm_stat is you wouldn't need to monitor it manually—you could review it, find when the swap started increasing, and see the memory use at that time.

Perhaps even cooler would be if you used Terminal to set an audible alarm to notify you when swap was increasing since then you could see exactly what you were doing when it started swapping, without the need to monitor swap manually. I'm sure that could be done with a shell script, but would either of you happen to know a Terminal one-liner for this?

Of course, as you know, if the OP wanted quantitative info. on how much swapping was going on, they would want to know the swap rate, in which case they would need to use vm_stat as I did here (coincidentally, in responding to an earlier query by @Analog Kid !):

 
Last edited:
  • Like
Reactions: Analog Kid

falainber

macrumors 68040
Mar 16, 2016
3,542
4,138
Wild West
The newer versions of Chrome have a feature to offload unused tabs’ memory usage, a lot of times that spills into the “cached files” section because I’ll see large sizes there and less in memory used. But I have about 3GB of swap on my 32GB M1 Pro and it kind of drives me crazy that sudo purge only cleans cached files and not swap. But I think running sudo purge with a large cached files would prevent going to swap as often.
In general the apps do not control swapping of virtual memory, OS does this. What Chrome is doing, it probably writes some of its data to disk (but not the OS swap files) and then frees virtual memory. So, it's app-specific memory swapping in a way. This way, it should reduce the use of memory and thus reduce or eliminate the need for OS to do memory swapping.
 

Analog Kid

macrumors G3
Mar 4, 2003
9,362
12,612
use Terminal to set an audible alarm to notify you when swap was being used
Sadly, I suspect someone could make a few bucks selling an app that sounds a klaxon when swap gets touched.

if the OP wanted quantitative info. on how much swapping was going on, they would want to know the swap rate, in which case they would need to use vm_stat, as I did here (coincidentally, in responding to an earlier query by @Analog Kid !):

https://forums.macrumors.com/thread...p.2379498/page-20?post=32076334#post-32076334

That's right, I forgot to answer back then! This was a good example of what swapping looks like when it is really an issue-- meaning your working dataset is larger than available RAM (in your case more than 128GB on a Studio, if I recall).

If you run vm_stat without arguments it tells you the page size. I'm not sure where your 4k/page number came from, it may have been me... Modern MacOS flavors have the page size at a whopping 16kB/page now, so I think you're actually swapping at about 1GB/sec. I don't know if that's because your application doesn't need to swap any faster, or if it's somehow limited by the small transaction random write speed of the SSD (the specs we usually see quoted are for GB sized files, not kB sized chunks).

Chrome tabs will not show this behavior... 😄
 
Last edited:
  • Like
Reactions: theorist9

Analog Kid

macrumors G3
Mar 4, 2003
9,362
12,612
In general the apps do not control swapping of virtual memory, OS does this. What Chrome is doing, it probably writes some of its data to disk (but not the OS swap files) and then frees virtual memory. So, it's app-specific memory swapping in a way. This way, it should reduce the use of memory and thus reduce or eliminate the need for OS to do memory swapping.
Photoshop does this too.
 

theorist9

macrumors 68040
May 28, 2015
3,883
3,067
Sadly, I suspect someone could make a few bucks selling an app that sounds a klaxon when swap gets touched.



That's right, I forgot to answer back then! This was a good example of what swapping looks like when it is really an issue-- meaning your working dataset is larger than available RAM (in your case more than 128GB on a Studio, if I recall).

If you run vm_stat without arguments it tells you the page size. I'm not sure where your 4k/page number came from, it may have been me... Modern MacOS flavors have the page size at a whopping 16kB/page now, so I think you're actually swapping at about 1GB/sec. I don't know if that's because your application doesn't need to swap any faster, or if it's somehow limited by the small transaction random write speed of the SSD (the specs we usually see quoted are for GB sized files, not kB sized chunks).

Chrome tabs will not show this behavior... 😄
I don't recall where I got the page size--I think it looked it up on Stackexchange; but it sounds like the info. was outdated or inapplicable to Macs.

Yeah, the 4k random write speeds to my WD SN850 SSD are ~400 MB/s (depending on queue depth), while those to my 2267 Mhz DDR4 RAM are ~1500 MB/s. Disappointingly, when I created a RAM disk, it wasn't that much faster than my SSD: ~500 MB/s.
 

mr_roboto

macrumors 6502a
Sep 30, 2020
856
1,869
Some applications are built to use swap no matter what you do, unless you severely tweak your operating system but that causes more harm than good.
This is a bad way of thinking about it because, at least in UNIX-derived operating systems like macOS (probably Windows too, I'm just not familiar), there's no API an app can call to manually evict pages to the swapfile, and thus no way to have them 'built to use swap'. Instead, swapping is an automated background service controlled by the OS. The only influence an app has over it is indirect - if it uses a lot of memory, it's liable to cause swapping to happen. But it can't directly control what gets swapped out, or when.

I think Apple, Microsoft and the Linux community know how to manage memory. Some bugs will always be there, but just don't worry about swap.
This is good advice. On the Mac in particular, just open Activity Monitor and if your memory pressure is red or yellow, sort processes by memory use to figure out what programs you can quit or browser tabs you can close to bring it back into the green.

Worrying about whether the swapfile is currently larger than 0 bytes (what the OP's post was concerned about) is pointless. Memory that's been swapped out doesn't get swapped back in until there's a need to do that, and if the OS is good at identifying memory unlikely to be needed again for a long time, many things that get swapped out stay there until a reboot.
 
  • Like
Reactions: Ethosik

Ethosik

Contributor
Oct 21, 2009
8,144
7,120
This is a bad way of thinking about it because, at least in UNIX-derived operating systems like macOS (probably Windows too, I'm just not familiar), there's no API an app can call to manually evict pages to the swapfile, and thus no way to have them 'built to use swap'. Instead, swapping is an automated background service controlled by the OS. The only influence an app has over it is indirect - if it uses a lot of memory, it's liable to cause swapping to happen. But it can't directly control what gets swapped out, or when.
There was something in Chrome for Windows's coding that forced to use Swap. It was confirmed a very long time ago back in the Windows 7 days. I no longer have the source. Whether it was something in the code that indirectly caused the OS to cause swap or not is irrelevant because it was still the code that caused it.
 

dmccloud

macrumors 68040
Sep 7, 2009
3,155
1,911
Anchorage, AK
There was something in Chrome for Windows's coding that forced to use Swap. It was confirmed a very long time ago back in the Windows 7 days. I no longer have the source. Whether it was something in the code that indirectly caused the OS to cause swap or not is irrelevant because it was still the code that caused it.

It may have been related to the background updater Chrome runs. That also caused a lot of problems when the M1 Macs first hit the market because that process ran unchecked and continuously, even if you hadn't launched Chrome at all since rebooting or powering on your machine. Google seemingly has a lot of issues with stuff like that which runs on the back end across all OSes.
 

ChrisA

macrumors G5
Jan 5, 2006
12,923
2,183
Redondo Beach, California
Back in the 1980s I was a university student and a computer science major. In class, they explained the answer to this exact question and in all those years the answer has not changed. Yes, there existed computers with virtual memory and swap files 40 years ago. In fact, the OS used as a teaching example is a SAME OS as found in a modern Mac. That would be BSD UNIX (which was released in the early 80s). MacOS is based on BSD UNIX and likely has code in it that is 40+ years old.

One goal of any modern (meaning post 1980s) operating system is to keep the data that is most likely to be needed in RAM and let the least likely data to remain on secondary storage (ie, disk). The answer is that simple. There are rules of thumb and "tricks" to estimate how likely it is that a chunk of data is likely to be accessed in the near future. The tricks and rules are evolving, but the goal is unchanged.

There is a way to compute the overall time to access data... Add the following:
1) the probability the data is in RAM times the speed (access time) of the RAM plus
2) the probability the data is on Disk (or SSD) times the speed of the disk (or SSD)

The OS tries to minimize the sum of #1 and #2. What we don't know is the tricks and rules of thumb MacOS uses. But I suspect the stuff that is swapped is data (not program code) that has not been accessed in a "long time"

Obviously the probability of being in RAM is higher if you have more RAM and the penalty for not being in RAM is less if you have a fast SSD to use as swap. Swapping makes the system faster.

Actually, the above is simplified. In real-life, the CPU might have a multi-level cache between the CPU and the RAM and the SSD might have some built-in RAM cache. So you can think of the memory as a 5-level pyramid where the top level is very fast but also very small and the lowest level is very big but slow. The hardware and software manage where the data lives inside the pyramid. Wasting space in the top with stuff you don't need is bad but so is not having it there when needed. Many engineering hours are spent trying to optimize this.
 
Last edited:

pshufd

macrumors G4
Oct 24, 2013
10,155
14,579
New Hampshire
Back in the 1980s I was a university student and a computer science major. In class, they explained the answer to this exact question and in all those years the answer has not changed. Yes, there existed computers with virtual memory and swap files 40 years ago. In fact, the OS used as a teaching example is a SAME OS as found in a modern Mac. That would be BSD UNIX (which was released in the early 80s). MacOS is based on BSD UNIX and likely has code in it that is 40+ years old.

One goal of any modern (meaning post 1980s) operating system is to keep the data that is most likely to be needed in RAM and let the least likely data to remain on secondary storage (ie, disk). The answer is that simple. There are rules of thumb and "tricks" to estimate how likely it is that a chunk of data is likely to be accessed in the near future. The tricks and rules are evolving, but the goal is unchanged.

There is a way to compute the overall time to access data... Add the following:
1) the probability the data is in RAM times the speed (access time) of the RAM plus
2) the probability the data is on Disk (or SSD) times the speed of the disk (or SSD)

The OS tries to minimize the sum of #1 and #2. What we don't know is the tricks and rules of thumb MacOS uses. But I suspect the stuff that is swapped is data (not program code) that has not been accessed in a "long time"

Obviously the probability of being in RAM is higher if you have more RAM and the penalty for not being in RAM is less if you have a fast SSD to use as swap. Swapping makes the system faster.

Actually, the above is simplified. In real-life, the CPU might have a multi-level cache between the CPU and the RAM and the SSD might have some built-in RAM cache. So you can think of the memory as a 5-level pyramid where the top level is very fast but also very small and the lowest level is very big but slow. The hardware and software manage where the data lives inside the pyramid. Wasting space in the top with stuff you don't need is bad but so is not having it there when needed. Many engineering hours are spent trying to optimize this.

In the old days, we wrote program overlays to save RAM. You can appreciate VM and swapping a lot more when you have to program overlays yourself.
 

Tagbert

macrumors 603
Jun 22, 2011
6,263
7,286
Seattle
All my swap is occupied by Opera and Chrome browsers. But the memory is enough, so I don't pay attention to it.
I don’t think that swap is anything to worry about if it doesn’t significantly impact performance. I just let my computer manage itself.
 

ctjack

macrumors 68000
Mar 8, 2020
1,565
1,575
Using chrome since it is noticeably faster than safari. Speedometer 2.0 confirms as well.
My 8gb swaps a lot on Monterey and swaps a little on BigSur as needed.
 

donawalt

Contributor
Sep 10, 2015
1,290
634
Using chrome since it is noticeably faster than safari. Speedometer 2.0 confirms as well.
Well not on my MacBook Pro - M2 Max, Ventura 13.4. I suspect you have extensions or something bogging down Safari. Here are my reads, you can see on the top left corner the first is Safari the second is Chrome. I ran these with nothing else running.

Screenshot 2023-06-18 at 7.49.16 PM.png

Screenshot 2023-06-18 at 7.50.06 PM.png
 
  • Like
Reactions: ctjack

ctjack

macrumors 68000
Mar 8, 2020
1,565
1,575
Good reference point. It is actually vice-versa, no extensions on Safari and 4 extensions in Chrome.
Probably has more to do with the BigSur safari and maybe m1. Can’t update OS just to get updated safari.
Well not on my MacBook Pro - M2 Max, Ventura 13.4. I suspect you have extensions or something bogging down Safari. Here are my reads, you can see on the top left corner the first is Safari the second is Chrome. I ran these with nothing else running.
 

spiderman0616

Suspended
Aug 1, 2010
5,670
7,499
I have the base model 14" and 16" M1 Pros, and I've never caught either of them using swap. I think my old base model M1 Air used it now and then, but that was to be expected.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.