Created
August 5, 2025 06:17
-
-
Save jacobsapps/f76070f888887808d5e1b9cc24f9a476 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// Gets current memory usage information for the app's memory footprint | |
/// Returns tuple with bytes used, total bytes available, and usage in MB | |
/// Uses task_vm_info to get accurate app-specific memory usage | |
func getMemoryUsage() -> (used: UInt64, total: UInt64, usedMB: Double) { | |
var taskInfo = task_vm_info_data_t() | |
var count = mach_msg_type_number_t(MemoryLayout<task_vm_info>.size) | |
let result: kern_return_t = withUnsafeMutablePointer( | |
to: &taskInfo | |
) { | |
$0.withMemoryRebound(to: integer_t.self, capacity: 1) { | |
task_info(mach_task_self_, | |
task_flavor_t(TASK_VM_INFO), | |
$0, | |
&count) | |
} | |
} | |
guard result == KERN_SUCCESS else { | |
return (used: 0, total: 0, usedMB: 0.0) | |
} | |
let totalMemory = ProcessInfo.processInfo.physicalMemory | |
let usedMemory = UInt64(taskInfo.phys_footprint) | |
let usedMB = Double(usedMemory) / (1024 * 1024) | |
return (used: usedMemory, total: totalMemory, usedMB: usedMB) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment