Skip to content

Instantly share code, notes, and snippets.

@loganblevins
Last active November 19, 2021 15:43
Show Gist options
  • Save loganblevins/11df4e95321caba318ab7578a93db51a to your computer and use it in GitHub Desktop.
Save loganblevins/11df4e95321caba318ab7578a93db51a to your computer and use it in GitHub Desktop.
Get current memory footprint, most resembling that of memory profiling tools in Xcode
// https://stackoverflow.com/a/57315975
// https://forums.developer.apple.com/thread/105088#357415
func memoryFootprint() -> mach_vm_size_t? {
// The `TASK_VM_INFO_COUNT` and `TASK_VM_INFO_REV1_COUNT` macros are too
// complex for the Swift C importer, so we have to define them ourselves.
let TASK_VM_INFO_COUNT = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<integer_t>.size)
let TASK_VM_INFO_REV1_COUNT = mach_msg_type_number_t(MemoryLayout.offset(of: \task_vm_info_data_t.min_address)! / MemoryLayout<integer_t>.size)
var info = task_vm_info_data_t()
var count = TASK_VM_INFO_COUNT
let kr = withUnsafeMutablePointer(to: &info) { infoPtr in
infoPtr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { intPtr in
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
}
func formattedMemoryFootprint() -> String {
let usedBytes: UInt64? = UInt64(self.memoryFootprint() ?? 0)
let usedMB = Double(usedBytes ?? 0) / 1024 / 1024
let usedMBAsString: String = "\(usedMB)MB"
return usedMBAsString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment