任何人都可以帮我获取iPhone的总使用内存吗?我正在开发一个应用程序来查找iPhone的所有细节。我找到了2-3个与此问题相关的帖子,但它们要么是obj-c,要么是不同的语法。我一直在努力将obj-c代码更改为swift,但却陷入了困境。
我正在尝试Available memory for iPhone OS app 但是一切看起来都很不一样。
马赫的语法也很难。任何人都可以提供一些例子来解释它。我可以从这里获取内存的所有信息https://github.com/Shmoopi/iOS-System-Services/blob/master/System%20Services/Utilities/SSMemoryInfo.m 但由于版权问题,我无法使用其他人的工作。我也不明白。
答案 0 :(得分:0)
你走了。这将打印出以字节为单位的已用内存。
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"))
}
答案 1 :(得分:0)
@Bikram 您可以使用以下函数来获取系统使用的总内存量 -
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")
}
}