简体   繁体   English

iPhone / iPad IOS App仪器内存计数与task_info内存计数

[英]iPhone / iPad IOS App Instrument Memory Count vs. task_info Memory Count

I have been using the Instruments Leak Tester and it gives a number for Total Allocations for an app around 1-3 meg. 我一直在使用仪器泄漏测试仪,它为一个大约1-3兆的应用程序提供了总分配数。

But, when using the task_info it is reporting much larger memory amounts like 10-20 meg. 但是,当使用task_info时,它会报告更大的内存量,如10-20 meg。

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正在返回某种总内存,包括堆栈/等,其中泄漏测试仪只报告Malloc / Alloc内存。

Also, why would the task_info number be increasing quite a bit during the app when the leak tester is not increasing that much.... 另外,为什么在应用程序期间,当泄漏测试仪没有增加那么多时,task_info数量会增加很多......

    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 (eg a library) will count as resident pages for the task. 即使属于(共享)内存映射文件(例如库)的页面也将被视为该任务的常驻页面。 But they will be ignored by the Leak Tester. 但泄漏测试仪会忽略它们。

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 (eg a shared library) and not all memory you allocate is necessarily resident in memory (eg a big malloc will not reserve physical memory for you right away, but only as soon as it is used). 并非所有可用内存都连接到您所做的实际分配(例如共享库),并且您分配的所有内存都不一定驻留在内存中(例如,一个大型malloc不会立即为您保留物理内存,但只能立即使用用来)。

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];

You can easily change this to mmap a file, by passing an fd to mmap and changing MAP_ANON to MAP_FILE . 通过将fd传递给mmap并将MAP_ANON更改为MAP_FILE ,可以轻松地将其更改为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, eg using a mmap (system) call just like the one above. 此外,大概是泄漏测试仪从malloc (库)调用向前看直到相应的free ,而实际的内存预留只是低一级,例如使用mmap (系统)调用就像上面那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM