以编程方式检索iPhone上的内存使用情况 [英] Programmatically retrieve memory usage on iPhone

查看:70
本文介绍了以编程方式检索iPhone上的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以程序化的方式检索iPhone应用程序正在使用的内存量。是的我知道ObjectAlloc / Leaks。我对这些不感兴趣,只知道是否可以编写一些代码,并获得正在使用的字节数量,并通过NSLog报告。

I'm trying to retrieve the amount of memory my iPhone app is using at anytime, programmatically. Yes I'm aware about ObjectAlloc/Leaks. I'm not interested in those, only to know if it's possible to write some code and get the amount of bytes being used and report it via NSLog.

谢谢。 / p>

Thanks.

推荐答案

要获取应用程序正在使用的内存的实际字节数,可以执行下面的示例。然而,你真的应该熟悉各种性能分析工具,以及它们旨在让您更好地了解使用情况。

To get the actual bytes of memory that your application is using, you can do something like the example below. However, you really should become familiar with the various profiling tools as well as they are designed to give you a much better picture of usage over-all.

#import <mach/mach.h>

// ...

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));
  }
}

结构中还有一个字段info.virtual_size这将给你可用的虚拟内存(或分配给您的应用程序作为潜在的虚拟内存在任何事件的内存)的字节数。 pgb链接到的代码将为您提供设备可用的内存量以及它是什么类型的内存。

There is also a field in the structure info.virtual_size which will give you the number of bytes available virtual memory (or memory allocated to your application as potential virtual memory in any event). The code that pgb links to will give you the amount of memory available to the device and what type of memory it is.

这篇关于以编程方式检索iPhone上的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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