Wednesday, March 25, 2015

OS X : Understanding RAM Usage

0. Understanding terminologies 

Left table 

Physical Memory :

        Actual RAM installed on machine ( aka Physical Memory ).

Memory Used :

        Amount of RAM used from physical.

Virtual Memory ( Non physical memory / spaced used from your HDD ) :

       Memory used by VM, includes swap as well as compressed data.

Swap Used :

       Amount of data currently compressed and moved to HDD for freeing physical memory.

Middle graph

Green :

       Good condition, everything should work just fine.

Yellow :

       Resources are not getting amount of memory required to run properly.

Red :

       Out of memory.

Right table

App Memory :

       Physical memory used by applications & system processes.

File Cache :

       Compressed files on physical memory. These are files from recently used application cached for faster access.

Wired Memory :

       This is data which will stay inside physical memory which is neither swappable nor it can be compressed.

Compressed :

       This is data on physical memory in compressed format as it was not used recently.

1. Understanding usage 

Since OS X 10.9, RAM utilisation techniques are completely different, Mavericks & Yosemite try to use all of your physical memory to speed up processes. So it doesn't matter whether you have 4 , 8 or 16 GB of RAM, most of the times you will see "Memory Used :" parameter from screenshot close to your physical memory.  But nothing to worry "File Cache :" & "Compressed" memory allocations will release memory in fractions of seconds if applications need more memory.

Efficient utilisation 

File Cache :

You can release this memory my command "sudo purge".

But, soon your system will start using this freed memory for caching again. This is quick solution for resolving hang like effect, as reaching file cache again to higher value takes some time.

Compressed :

This should be close to zero most of the times, if you see it crossed around 1 GB, restart your machine. Not shutting down or restarting machine for long period of time keeps on adding compressed memory.

Disk Usage :

Keep around 40 GB of free HDD space for smoother operations. Lesser HDD space results into less use of virtual memory, it also slows down read-writes, which affects overall performance of system.

Closing unnecessary processes :

Click on memory title from statistics table, this will sort memory usage from Highest to Lowest order, close all unnecessary user application by targeting high memory using application first.

2. Geek zone

How to disable compressed memory ?

vm/vm_pageout.h defines the modes for the vm_compressor boot argument.
Default value is VM_PAGER_COMPRESSOR_WITH_SWAP ( from vm/vm_compressor.c )

To disable compression change vm_compressor_mode argument to 1 ( VM_PAGER_DEFAULT )

       sudo nvram boot-args="vm_compressor=1"

Restart machine & verify change is successful

       sysctl -a vm.compressor_mode

To re-enable compression change vm_compressor_mode argument to 4 ( VM_PAGER_COMPRESSOR_WITH_SWAP )

       sudo nvram boot-args="vm_compressor=4"

How is memory pressure calculated ?

Memory pressure is defined by two counters Mach keeps internally: 

vm_page_free_count: How many pages of RAM are presently free 
vm_page_free_target: How many pages of RAM, at a minimum, should optimally be free.


sysctl -a vm | grep page_free


vm.vm_page_free_target: 4000
vm.page_free_wanted: 0
vm.page_free_count: 198477

Pressure levels


sysctl kern.memorystatus_vm_pressure_level


kern.memorystatus_vm_pressure_level: 1

level 1 : Normal (Green)
level 2 : Warning (Yellow)
level 4 : Critical (Red)

Changing memory pressure levels


Normal : Graph should change to green levels
sudo memory_pressure -l normal

Warning : Graph should change to steady yellow levels
sudo memory_pressure -l warn

Critical : Graph should change to red levels
sudo memory_pressure -l critical

Other commands 


In detail virtual Memory Statistics.


In detail pages & swap info.

No comments:

Post a Comment