我的iPad运行时内存在哪里? [英] Where is my iPad runtime memory going?

查看:225
本文介绍了我的iPad运行时内存在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显然导致设备(iPad)耗尽内存,因此它放弃了我的应用程序。我正在试图理解正在发生的事情,因为乐器告诉我我正在使用大约80Mb,并且该设备上没有其他应用运行。

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发现这段代码片段要求iOS下的Mach系统获取内存统计信息:

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

当我使用此函数获取三个内存值时,我发现即使mem_used总数没有那么大的改变,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

所以全部一个suden我从157MB的可用内存到10MB的可用内存,但我的使用率仅从78MB增加到84MB。总内存从236MB减少到94MB。

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.

在两个内存之间执行的所有代码检查是本机C ++代码,它与任何Apple框架都没有任何交互。确实有很多很多调用内存系统从C ++堆中分配和释放对象,但是如图所示,最后只分配了大约4MB的额外内存,其余的都被释放/删除。

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?

谢谢,

-Eric

推荐答案

您应该使用 task_info 而不是 host_statistics 来检索应用程序的内存使用情况:

You should use task_info instead of host_statistics to retrieve memory usage of your application:

# include <mach/mach.h>
# include <mach/mach_host.h>

void dump_memory_usage() {
  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( @"task_info: 0x%08lx 0x%08lx\n", info.virtual_size, info.resident_size );
  }
  else {
    NSLog( @"task_info failed with error %ld ( 0x%08lx ), '%s'\n", kerr, kerr, mach_error_string( kerr ) );
  }
}

这篇关于我的iPad运行时内存在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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