Skip to content

Instantly share code, notes, and snippets.

@steipete
Last active March 5, 2024 09:15
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save steipete/33af275cc1cb419b0f01 to your computer and use it in GitHub Desktop.
Save steipete/33af275cc1cb419b0f01 to your computer and use it in GitHub Desktop.
Example for DISPATCH_SOURCE_TYPE_MEMORYPRESSURE (Mac only, since 10.9) ... Since we share code between iOS and mac, I'm trying to be a good system citizen and reimplement the equivalent of UIApplicationDidReceiveMemoryWarningNotification on the Mac.
NSString *const PSPDFApplicationDidReceiveMemoryWarningNotification = @"PSPDFApplicationDidReceiveMemoryWarningNotification";
// Test with sudo memory_pressure -l critical. Don't forget to re-set afterwards!
__attribute__((constructor)) static void PSPDFInstallLowMemoryNotificationWarningMac(void) {
static dispatch_source_t source;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
dispatch_source_memorypressure_flags_t pressureLevel = dispatch_source_get_data(source);
[NSNotificationCenter.defaultCenter postNotificationName:PSPDFApplicationDidReceiveMemoryWarningNotification object:nil userInfo:@{@"pressure": @(pressureLevel)}];
});
dispatch_resume(source);
});
}
@gabebear
Copy link

gabebear commented May 5, 2015

This seems to be crashing on iOS7.

DISPATCH_SOURCE_TYPE_MEMORYPRESSURE existed in iOS7, but the iOS8.3 headers now have __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_8_0) for it...

@WhitebrowSwordsman
Copy link

This not work on macos 12.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment