Last active
August 12, 2024 10:18
-
-
Save T1T4N/2b17129bd6f7b53bb599754bb1fff666 to your computer and use it in GitHub Desktop.
Preconcurrency (Swift 6) way of monitoring memory pressure in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@discardableResult | |
@preconcurrency func logMemoryPressure() -> DispatchSourceProtocol { | |
let source = DispatchSource.makeMemoryPressureSource(eventMask: .all, queue: nil) | |
let queue = DispatchQueue( | |
label: "com.example.memoryPressure") | |
queue.async { | |
source.setEventHandler { | |
guard !source.isCancelled else { return } | |
let event = source.data | |
switch event { | |
case .normal: | |
os_log("Memory pressure: %{public}s", log: .default, type: .debug, "normal") | |
case .warning: | |
os_log("Memory pressure: %{public}s", log: .default, type: .info, "warning") | |
case .critical: | |
os_log("Memory pressure: %{public}s", log: .default, type: .error, "critical") | |
default: | |
break | |
} | |
} | |
os_log("Memory pressure logging start", log: .default, type: .debug) | |
source.activate() | |
} | |
return source | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment