Get memory usage

I'm trying to get the memory usage of the entire system.

// referance: https://www.jb51.cc/iOS/590624.html
public func memsize() -> UInt64 {
    var taskInfo = mach_task_basic_info()
    var count = UInt32(MemoryLayout<mach_task_basic_info>.size)
    let kerr: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
        $0.withMemoryRebound(to: integer_t.self,capacity: 1) {
            task_info(mach_task_self_,task_flavor_t(MACH_TASK_BASIC_INFO),$0,&count)
        }
    }
 
    if kerr == KERN_SUCCESS {
        return taskInfo.resident_size
    }
    return 0
}

not working, apparently. When my system uses 15 GB it shows it's below 1 GB.

Do anyone have other ways to get memory usage of the device (better macOS)

task_info returns information about a specific task. In Mach:

  • A task corresponds to a BSD process.

  • The machine as a whole is a host.

To get host info, call… you guessed it!… host_info. There’s also host_statistics, which is more likely to be interesting to you.

IIRC you’re planning to show this stuff to the user, and that’s fine.

However, for other folks reading along, I recommend that you not use these values to drive your code. I talk about this in some detail in On Free Memory.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Get memory usage
 
 
Q