iPhone / iPad IOS 应用程序仪器内存计数与 task_info 内存计数
我一直在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些数字无法真正进行比较。即使属于(共享)内存映射文件(例如库)的页面也将被视为任务的常驻页面。但泄漏测试仪将忽略它们。
需要注意的重要一点是,进程可用的内存(以任何方式:只读、读/写、可执行或不可执行)和您在程序中分配的内存之间存在概念差异。并非所有可用内存都连接到您所做的实际分配(例如共享库),并且并非您分配的所有内存都一定驻留在内存中(例如,大的 malloc 不会立即为您保留物理内存,但只有当它被使用)。
您可以通过映射匿名内存区域(或文件)来测试其影响:
您可以通过将 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:
You can easily change this to
mmap
a file, by passing an fd tommap
and changingMAP_ANON
toMAP_FILE
.Also, presumably the leak tester looks from the
malloc
(library) call onward until a correspondingfree
, while the actual memory reservation is done just one level lower, e.g. using ammap
(system) call just like the one above.