MacOS – How is the Y value for the Memory Pressure graph in Activity Monitor calculated

macosmemoryvirtual-memory

How is vm.memory_pressure's value bounded / calculated on OS X 10.10.2 – Yosemite?

I've researched several answers to this question and am trying a few things on the command line to get exact values rather than looking at the graphs in Activity Monitor.

With the system under light load:

~ ❯❯❯ sysctl -a vm | grep page_free && sysctl vm.memory_pressure && sysctl kern.memorystatus_vm_pressure_level
vm.vm_page_free_target: 4000
vm.page_free_wanted: 0
vm.page_free_count: 2683717
vm.memory_pressure: 0
kern.memorystatus_vm_pressure_level: 1

The kern.memorystatus_vm_pressure_level looks right, and appears to be what it uses to color the graph (green for 1-NORMAL, yellow for 2-WARN, red for 4-CRITICAL). However, vm.memory_pressure is 0 and the graph has some amount of green to it, and it's jagged, meaning the value is clearly changing over time, not just sitting at 0.

Under heavy load it looks like this:

❯❯❯ sysctl -a vm | grep page_free && sysctl vm.memory_pressure && sysctl kern.memorystatus_vm_pressure_level
vm.vm_page_free_target: 4000
vm.page_free_wanted: 0
vm.page_free_count: 32572
vm.memory_pressure: 1693
kern.memorystatus_vm_pressure_level: 4

Or this…

~ ❯❯❯ sysctl -a vm | grep page_free && sysctl vm.memory_pressure && sysctl kern.memorystatus_vm_pressure_level
vm.vm_page_free_target: 4000
vm.page_free_wanted: 0
vm.page_free_count: 5431
vm.memory_pressure: 277
kern.memorystatus_vm_pressure_level: 4

Or this…

~ ❯❯❯ sysctl -a vm | grep page_free && sysctl vm.memory_pressure && sysctl kern.memorystatus_vm_pressure_level
vm.vm_page_free_target: 4000
vm.page_free_wanted: 3220
vm.page_free_count: 805
vm.memory_pressure: 1
kern.memorystatus_vm_pressure_level: 4

Here's where I'm confused. None of these values alone could possibly be the Y value of the graph for a couple of reasons.

First, vm.memory_pressure just sits at 0 almost all the time under normal load, and the graph doesn't sit at 0.

Second, they are not consistent with one another. Looking at the memory_pressure value, it is lower when it actually had wanted pages (3220 wanted and pressure of only 1) than when it had 5431 free and none wanted (pressure was 277). And somehow that's even lower pressure than when it had 32572 free (and had 1693 pressure).

So, when it plots that graph, what's the calculation for the Y value, and how can I get the data I need to calculate it from the command line? I'm looking to make a command line tool that will spit out a percentage of how high the current line would be on that graph, so I need to know how to calculate the value at any given time, as well as what the range of possible values are.

Best Answer

This is currently only a partial answer. This answer not complete until this sentence removed.

The thing about this question is that on the face of it, the Memory Pressure graph doesn't quite make sense when comparing it with what's reported in Terminal when using the command outlined by the OP:

sysctl -a vm | grep page_free && sysctl vm.memory_pressure && sysctl kern.memorystatus_vm_pressure_level

However, the problem here is that this command limits the vm values it's reporting back. In fact, there's actually 105 values in total but this command only reports the following five values:

vm.vm_page_free_target:
vm.page_free_wanted:
vm.page_free_count:
vm.memory_pressure:
kern.memorystatus_vm_pressure_level:

NOTE: Numerical values would normally appear after the colon (:), however I have not included these.

Obviously, the overwhelming majority of the 105 vm values are not part of the actual calculation that determines memory pressure, but the problem here is that the five values above are not the only ones used to calculate memory pressure.

For example, while the above output provides some of the various free values, it's not providing any of the inactive values, etc.

These values are important because the memory pressure algorithm calculates a ratio between those values, and the relationship between those values isn't apparent from the output produced by the command in the OP's question.

I think I may have some technical information that can shed more light on this. I will update this answer either way within the week.

[STATUS UPDATE]

I've been unable to find the technical information I was looking for. However, I am fairly certain I do still have it - probably on some old CDR/DVDR backups. I will update this answer once I've found it, or after I'm certain I won't find it.