Skip to content

Instantly share code, notes, and snippets.

@tinder-tannerbennett
Created September 17, 2021 21:22
Show Gist options
  • Save tinder-tannerbennett/37451b28d2dc1b23c5de1e58cb4837a0 to your computer and use it in GitHub Desktop.
Save tinder-tannerbennett/37451b28d2dc1b23c5de1e58cb4837a0 to your computer and use it in GitHub Desktop.
Get the memory footprint of the current process. Taken from StackOverflow.
func memoryFootprint() -> mach_vm_size_t? {
let integer_t_size = MemoryLayout<integer_t>.size
let count = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / integer_t_size)
let TASK_VM_INFO_REV1_COUNT = mach_msg_type_number_t(MemoryLayout.offset(of: \task_vm_info_data_t.min_address)! / integer_t_size)
var info = task_vm_info_data_t()
let kr = withUnsafeMutablePointer(to: &info) { infoPtr in
infoPtr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { intPtr in
return task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), intPtr, &count)
}
}
guard kr == KERN_SUCCESS, count >= TASK_VM_INFO_REV1_COUNT else {
return nil
}
return info.phys_footprint
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment