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

查看:22
本文介绍了以编程方式检索 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.

谢谢.

推荐答案

要获取应用程序使用的实际内存字节数,您可以执行类似于以下示例的操作.但是,您确实应该熟悉各种分析工具,它们旨在让您更好地了解总体使用情况.

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 = TASK_BASIC_INFO_COUNT;
  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): %lu", info.resident_size);
    NSLog(@"Memory in use (in MiB): %f", ((CGFloat)info.resident_size / 1048576));
  } 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天全站免登陆