On macOS (checked on 10.11 & 10.12), if I track memory pressure changes with code like
_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_NORMAL | DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0));
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(_source, ^{
typeof(self) strongSelf = weakSelf;
dispatch_source_memorypressure_flags_t pressureLevel = dispatch_source_get_data(strongSelf.source);
NSLog(@"Memory pressure level: %lu", pressureLevel);
});
dispatch_resume(_source);
and use the memory_pressure command line tool, or use a virtual machine to simulate different RAM usage scenarios, I notice I will indeed get DISPATCH_MEMORYPRESSURE_WARN and DISPATCH_MEMORYPRESSURE_CRITICAL memory pressure changes, but I never receive DISPATCH_MEMORYPRESSURE_NORMAL when it goes back to normal.
Also, if an application is started while the memory pressure is already in an elevated state, it will never report that with this mechanism. There is no means to obtain the memory pressure "right now".
Is there an accurate method to get the correct memory pressure on macOS?