iPhone / iPad IOS 应用程序仪器内存计数与 task_info 内存计数

发布于 2024-12-02 21:03:29 字数 643 浏览 1 评论 0原文

我一直在使用 Instruments Leak Tester,它给出了大约 1-3 meg 的应用程序总分配数字。

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

我想我只是想确认task_info正在返回某种总内存,包括堆栈/等,其中泄漏测试器只是报告Malloc/Alloc内存。

另外,为什么当泄漏测试仪没有增加那么多时,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));
    }

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

But, when using the task_info it is reporting much larger memory amounts like 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-12-09 21:03:29

这些数字无法真正进行比较。即使属于(共享)内存映射文件(例如库)的页面也将被视为任务的常驻页面。但泄漏测试仪将忽略它们。

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

您可以通过映射匿名内存区域(或文件)来测试其影响:

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

您可以通过将 fd 传递给 mmapmmap 文件> 并将 MAP_ANON 更改为 MAP_FILE

此外,大概泄漏测试器会从malloc(库)调用向前查找,直到相应的free,而实际的内存保留仅低一级完成,例如使用< code>mmap (系统)调用就像上面的调用一样。

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.

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文