我的 iPad 运行时内存去哪儿了?
我导致设备(iPad)明显耗尽内存,所以它放弃了我的应用程序。我试图了解发生了什么,因为 Instruments 告诉我我正在使用大约 80Mb,并且设备上没有运行其他应用程序。
我发现这个代码片段向 iOS 下的 Mach 系统询问内存统计信息:
#import <mach/mach.h>
#import <mach/mach_host.h>
static void print_free_memory () {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
NSLog(@"Failed to fetch vm statistics");
/* Stats in bytes */
natural_t mem_used = (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * pagesize;
natural_t mem_free = vm_stat.free_count * pagesize;
natural_t mem_total = mem_used + mem_free;
NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
}
当我使用这个函数获取三个内存值时,我发现 mem_total 值正在下降,尽管 mem_used 总数没有改变很多。这里有两个连续的输出行:
<Warning>: used: 78585856 free: 157941760 total: 236527616
一些代码执行....
<Warning>: used 83976192 free: 10551296 total: 94527488
所以我突然从 157MB 的可用内存增加到 10MB 的可用内存,但我的使用量只从 78MB 增加到 84MB。总内存从 236MB 减少到 94MB。
这对任何人来说都有意义吗?在此期间,设备上没有其他应用程序运行,该设备基本上应该完全专用于我的应用程序。
在两次内存检查之间执行的所有代码都是本机 C++ 代码,与任何 Apple 框架没有任何交互。确实有很多很多调用内存系统来从 C++ 堆中分配和释放对象,但正如所见,最终只分配了大约 4MB 的额外内存,其余的都被释放/删除。
丢失的内存会不会被堆碎片消耗掉了?即,堆是否只是如此碎片化,以至于块开销消耗了所有额外的、未计算的内存?
还有其他人看到过这种行为吗?
谢谢,
-埃里克
I'm causing the device (iPad) to run out of memory apparently, so it is jettisoning my app. I'm trying to understand what is going on as Instruments is telling me that I'm using about 80Mb, and there is no other app running on the device.
I found this code snippet to ask the Mach system under iOS for the memory stats:
#import <mach/mach.h>
#import <mach/mach_host.h>
static void print_free_memory () {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
NSLog(@"Failed to fetch vm statistics");
/* Stats in bytes */
natural_t mem_used = (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * pagesize;
natural_t mem_free = vm_stat.free_count * pagesize;
natural_t mem_total = mem_used + mem_free;
NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
}
When I use this function to obtain the three memory values, I am finding that the mem_total value is going way down even though the mem_used total is not changing all that much. Here are two successive output lines:
<Warning>: used: 78585856 free: 157941760 total: 236527616
some code executes....
<Warning>: used 83976192 free: 10551296 total: 94527488
So all of a suden I go from 157MB of free memory to 10MB of free memory, but my usage only increased from 78MB to 84MB. The total memory decreased from 236MB to 94MB.
Does this make sense to anybody? There are no other applications running on the device during this time period, the device should be basically totally dedicated to my application.
All of the code that executes between the two memory checks is native C++ code that has no interaction at all with any Apple framework. There are indeed many, many calls to the memory system to allocate and deallocate objects from the C++ heap, but as seen, only about 4MB of additional memory is in the end allocated, all the rest is freed / deleted.
Can it be that the missing memory is being consumed by heap fragmentation? I.e. is the heap simply so fragmented that the block overhead is consuming all of the additional, unaccounted, memory?
Has anyone else seen this behavior?
Thanks,
-Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
task_info
而不是host_statistics
来检索应用程序的内存使用情况:You should use
task_info
instead ofhost_statistics
to retrieve memory usage of your application: