如何在Swift中获取内存使用情况 [英] How to get memory usage in Swift

查看:821
本文介绍了如何在Swift中获取内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Swift中以编程方式获取我的应用程序的内存使用量(MB)。我找到了Objective-C的示例代码。任何人都可以帮助我将其转换为Swift。谢谢!

I need to get the memory usage (MB) for my app programmatically in Swift. I found sample code for Objective-C. Anyone can help me to convert this into Swift. Thanks!

void report_memory(void) {
  struct task_basic_info info;
  mach_msg_type_number_t size = sizeof(info);
  kern_return_t kerr = task_info(mach_task_self(),
                             TASK_BASIC_INFO,
                             (task_info_t)&info,
                             &size);
  if( kerr == KERN_SUCCESS ) {
    NSLog(@"Memory in use (in bytes): %u", info.resident_size);
  } else {
    NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
  }
}


推荐答案

编辑:oops,应该先检查重复项,Nate已经回答了这个问题已经这里。虽然你可能想要注意我的版本中的细微差别,它使用 withUnsafeMutablePointer

edit: oops, should have checked for duplicates first, Nate answered this question already here. Though you might want to note the slight difference in my version, which uses withUnsafeMutablePointer.

你正在被投票因为人们不喜欢我怎么能将这个Obj-C代码转换为Swift的问题,但是在这种情况下它可能是公平的,因为这个特定的转换特别毛茸茸。我认为这是有效的:

You’re getting downvoted since people don’t like "How can I convert this Obj-C code to Swift" questions, however in this case it might be fair enough since this this specific conversion is particularly hairy. I think this is working:

func report_memory() {
    var info = task_basic_info()
    var count = mach_msg_type_number_t(sizeofValue(info))/4

    let kerr: kern_return_t = withUnsafeMutablePointer(&info) {

        task_info(mach_task_self_,
            task_flavor_t(TASK_BASIC_INFO),
            task_info_t($0),
            &count)

    }

    if kerr == KERN_SUCCESS {
        println("Memory in use (in bytes): \(info.resident_size)")
    }
    else {
        println("Error with task_info(): " +
            (String.fromCString(mach_error_string(kerr)) ?? "unknown error"))
    }
}

有几个基本的强制 - sizeof 返回 Int ,但函数调用需要 UInt32的。类似地(并且稍微更令人愤怒), TASK_BASIC_INFO Int32 ,但是呼叫需要 UInt32

There’s a couple of basic coercions – sizeof returns an Int, but the function call needs a UInt32. Similarly (and slightly more infuriatingly), TASK_BASIC_INFO is an Int32, but the call needs a UInt32.

令人讨厌的部分是传递第三个参数。 task_info 根据您想要的信息类型,指向不同大小的多种不同结构。所以你需要从 task_basic_info 对象中获取一个指针,然后将其转换为特定类型的指针 task_info 实际上需要(也就是说,一旦你浏览了所有的typedef,指向 UInt32 )。

The nasty part is passing in the third parameter. task_info takes a pointer to multiple different kinds of structs of different sizes depending on what kind of info you want. So you need to get a pointer from your task_basic_info object, then cast it to the specific kind of pointer task_info actually wants (which is, once you wade through all the typedefs, a pointer to UInt32).

task_info 说最后一个 count 参数应该是task_info中最大整数数的计数,但是当它表示整数我猜这意味着32位整数因此除以4.

The docs for task_info say that the last count parameter is supposed to be a count of the "maximum number of integers in task_info" but when it says integers I guess it means 32-bit integers hence divide by 4.

这篇关于如何在Swift中获取内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆