2

Can anyone help me get the total used memory of iPhone? I am working on an app to find all the details of iPhone. I have found 2-3 posts related to this question but they are either in Obj-C or a different language/syntaxsyntax. I have been working a lot to change the obj-c code to swift but got stuck somewhere down the path.

I am trying Available memory for iPhone OS app but everything looks different in Swift.

Also the syntax of mach is pretty much difficult. Can anybody provide some example with some explanation on it. I can get the all the information of the memory from here https://github.com/Shmoopi/iOS-System-Services/blob/master/System%20Services/Utilities/SSMemoryInfo.m But due to the copyright issue I can't use other's work. Also I don't understand anything from there.

Alex Zavatone
  • 4,106
  • 36
  • 54
Jivan Bhandari
  • 860
  • 1
  • 10
  • 32

2 Answers2

2

Here you go. This will print out used memory in bytes.

var info = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4

let kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
    $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 {
    print("Memory in use (in bytes): \(info.resident_size)")
}
else {
    print("Error with task_info(): " +
        (String(cString: mach_error_string(kerr), encoding: String.Encoding.ascii) ?? "unknown error"))
}
Fangming
  • 24,551
  • 6
  • 100
  • 90
  • 1
    I already came across the above solution but it only gives output as 30 mb. I think it's giving the memory usage of current application only. However, I need to get total memory used by all the application. – Jivan Bhandari Jun 25 '17 at 05:36
  • @Bikram How are you using this function. Here is a screen shot on a new project I just created with empty everything and just this function. I am printing out 276 MB right now. http://imgur.com/a/G21ic – Fangming Jun 25 '17 at 10:50
  • @Ning ya that's right. But when I use other application to see memory usage then It shows me 1555.0 MB. I think 1555 MB is correct because iphone can't run on 276 MB only. Correct me if I am wrong – Jivan Bhandari Jun 25 '17 at 11:41
2

@Bikram You can use below function to get total amount of memory used by system-

   func getUsedMemory() {
     var usedMemory: Int64 = 0
            let hostPort: mach_port_t = mach_host_self()
            var host_size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<vm_statistics_data_t>.stride / MemoryLayout<integer_t>.stride)
            var pagesize:vm_size_t = 0
            host_page_size(hostPort, &pagesize)
            var vmStat: vm_statistics = vm_statistics_data_t()

            let status: kern_return_t = withUnsafeMutableBytes(of: &vmStat) {
                let boundPtr = $0.baseAddress?.bindMemory(to: Int32.self, capacity: MemoryLayout.size(ofValue: vmStat) / MemoryLayout<Int32>.stride)
                return host_statistics(hostPort, HOST_VM_INFO, boundPtr, &host_size)
            }
            // Now take a look at what we got and compare it against KERN_SUCCESS
            if status == KERN_SUCCESS {
                usedMemory = (Int64)((vm_size_t)(vmStat.active_count + vmStat.inactive_count + vmStat.wire_count) * pagesize)
            }
            else {
              log("Failed to get Virtual memory inforor")
            }
   }
srus2017
  • 394
  • 2
  • 14
  • I'm getting this error when using code: `Overlapping accesses to 'vmStat', but modification requires exclusive access; consider copying to a local variable` any ideas? – denislexic Jul 10 '19 at 07:56
  • Foy anybody else getting the warning mentioned above. You need to create a local variable for the capacity, so `let capacity = MemoryLayout.size(ofValue: vmStat) / MemoryLayout.stride` and then in the `.bindMemory(to: Int32.self, capacity: capacity ).....` – denislexic Jul 10 '19 at 17:42