is this swap memory use normal?

I got myself a m1 macbook air with 8 gb of ram
Ive been using it for a month now and noticed that my swap usage is unusally high, is that something to worry about?

As you can see from the screenshot swap is around 3 gigs, and it hasnt gone done down yet.

enter image description here
I was hoping that 8 gigs was enough for vscode and node but now I doubt it

Also, my ram usage in the activity monitor always peaks at 6.6-6.7 gigs, never goes up more than that.

enter image description here labels are: all ram, used ram, cache, swap

Should I be concerned?


Solution 1:

So long as memory pressure looks OK, there's nothing to worry about.

macOS keeps things cached in RAM as long as that RAM is not needed by anything else - that gives it a tendency overall to look like it fills up & never empties.

The same can be said of swap. It fills, the contents may change, but the swap figure displayed is always a "high tide" figure. You never see it go down until you reboot.

It doesn't serve you or the OS well at all if it were to periodically flush unused RAM or swap just to "look tidier". Its purpose is best served by it just hanging onto it all in case it's needed again.

Solution 2:

There are two major misconceptions that are often seen in relation to memory management:

  • Full RAM is bad.
  • Full swap is bad.

Both are wrong.

RAM is extremely fast compared to other types of storage, both latency-wise and bandwidth-wise. RAM is also extremely expensive compared to other types of storage. Hence, you want RAM to be always full, otherwise you are wasting fast and expensive storage.

Swapping is bad. Swap isn't. Most OSs will flush pages from RAM to swap preemptively during periods of low system load but keep them in RAM at the same time as well. Why? Because swapping is slow, and thus you want stuff be already in swap in case the proverbial feces hit the proverbial air circulation unit. You don't want your system to start swapping out at the same time when it is already under high load anyway.

So, an OS will try to use as much RAM as possible by e.g. caching recently and regularly accessed files in RAM. And it will try to use as much swap as possible by preemptively copying data from RAM to swap, so that if the OS needs the RAM quickly, it can simply use it for a different purpose without having to save it to swap first.

Now, when I wrote above that the OS will try to use "as much RAM as possible", that is not 100% true. It is more like the OS will try to use "as much RAM as it deems sensible", and defining what, precisely, "sensible" means is one of the things that performance engineers and kernel developers have been trying to improve upon ever since memory management was invented some 70 years ago.

MacOS in, particular, will try to keep some amount of RAM free, and will also compress some parts of RAM.