iPhone / iPad IOS App仪器内存计数与task_info内存计数 [英] iPhone / iPad IOS App Instrument Memory Count vs. task_info Memory Count

查看:511
本文介绍了iPhone / iPad IOS App仪器内存计数与task_info内存计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用仪器泄漏测试仪,它为应用程序提供了大约1-3兆的总分配数。

I have been using the Instruments Leak Tester and it gives a number for Total Allocations for an app around 1-3 meg.

但是,当使用task_info时报告更大的内存量,如10-20 meg。

But, when using the task_info it is reporting much larger memory amounts like 10-20 meg.

我想我只想确认task_info是否返回某种总内存,包括堆栈/等等泄密测试仪只是报告了Malloc / Alloc内存。

I guess I just want to confirm that the task_info is returning some sort of total memory including stack / etc where the leak tester is just reporting Malloc / Alloc memory.

另外,为什么在应用程序期间,当泄漏测试仪没有增加那么多时,task_info数量会增加很多。 ...

Also, why would the task_info number be increasing quite a bit during the app when the leak tester is not increasing that much....

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


推荐答案

这些数字无法真正比​​较。甚至属于(共享的)存储器映射文件(例如库)的页面也将被视为该任务的驻留页面。但泄漏测试仪会忽略它们。

Those numbers cannot be compared really. Even pages belonging to a (shared) memory mapped file (e.g. a library) will count as resident pages for the task. But they will be ignored by the Leak Tester.

需要注意的重要一点是,过程可用的内存之间存在概念上的差异(以任何方式:readonly,读/写,可执行或不可执行)和您在程序中分配的内存。并非所有可用内存都连接到您所做的实际分配(例如共享库),并且您分配的所有内存都不一定驻留在内存中(例如,大型malloc不会立即为您保留物理内存,但只能立即为它使用)。

The important thing to note is that there is a conceptual difference between memory available to the process (in whatever way: readonly, read/write, executable or not) and memory allocated by you, within your program. Not all available memory is connected to an actual allocation you did (e.g. a shared library) and not all memory you allocate is necessarily resident in memory (e.g. a big malloc will not reserve physical memory for you right away, but only as soon as it is used).

您可以使用以下方法测试内存(或文件)的匿名区域来测试其影响:

You can test the impact of this by mapping an anonymous region of memory (or a file) using:

#include <sys/mman.h>

// allocate anonymous region of memory (1 mb)
char *p = mmap(NULL,1024*1024,PROT_WRITE|PROT_READ,MAP_PRIVATE|MAP_ANON,0,0);

// actually access the memory, or it will not be resident
int sum=0;
for(int i=0;i<1024*1024;i++ ) sum += p[i];

您可以轻松将其更改为 mmap a通过将fd传递给 mmap 并将 MAP_ANON 更改为 MAP_FILE

You can easily change this to mmap a file, by passing an fd to mmap and changing MAP_ANON to MAP_FILE.

另外,大概是泄密测试仪从 malloc (库)调用开始直到相应的免费,而实际的内存预留只需降低一级,例如使用 mmap (系统)调用就像上面那样。

Also, presumably the leak tester looks from the malloc (library) call onward until a corresponding free, while the actual memory reservation is done just one level lower, e.g. using a mmap (system) call just like the one above.

这篇关于iPhone / iPad IOS App仪器内存计数与task_info内存计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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