macOS memory usage/management

Not long ago I was learning about macOS memory management and had written this elsewhere. I hope this is useful and do let me know if I get any of the details wrong!

The high RAM usage is an inherent feature of macOS’s memory management system. macOS (whose kernel is derived from Mach kernel) features “a fully integrated virtual memory system” that is always on. The virtual memory system comprises the physical RAM itself (the expensive ones) and drive storage (also expensive but much cheaper). The memory paging scheme maximises system performance by looking at app profiles and memory usage pattern, then preemptively loads pages (memory blocks) which the apps might use. This usage pattern can be built up over time as you use the app, or the app developers can specify it.

The paging system is configured such that it loads and retains pages in memory. Pages that are recently accessed are marked as active, otherwise they become inactive. When more memory is required by apps and there are no longer free memory, inactive pages are removed to make way for new active ones. The removal can be a complete purge, or they may be compressed or swapped. The exception is wired memory which is required by the kernel and its data structure, and cannot be paged out. This arrangement maximises system performance since loading data into memory takes much longer than paging existing ones in memory. Hence the adage “unused memory is wasted memory”.

The memory pressure chart in Activity Monitor is the summary of the above interactions. Activity Monitor can indicate “high memory usage” but low on memory pressure, simply because of the qualities described above. Memory pressure turns yellow or red when macOS cannot efficiently use memory (because of high swap/compression-decompression caused by lack of RAM).

8 Likes