为什么mmap在iOS上失败? [英] Why does mmap fail on iOS?

查看:68
本文介绍了为什么mmap在iOS上失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mmap在iOS上读取和播放音频文件.它适用于最大约400MB的文件.但是,当我尝试500MB文件时,出现ENOMEM错误.

I'm trying to use mmap to read and play audio files on iOS. It works fine for files up to about 400MB. But when I try a 500MB file, I get a ENOMEM error.

char *path = [[[NSBundle mainBundle] pathForResource: @"test500MB" ofType: @"wav"] cStringUsingEncoding: [NSString defaultCStringEncoding]];
FILE *f = fopen( path, "rb" );
fseek( f, 0, SEEK_END );
int len = (int)ftell( f );
fseek( f, 0, SEEK_SET );

void *raw = mmap( 0, len, PROT_READ, MAP_SHARED, fileno( f ), 0 );

if ( raw == MAP_FAILED ) {
    printf( "MAP_FAILED. errno=%d", errno ); // Here it says 12, which is ENOMEM.
}

为什么?

我对诸如"700MB是虚拟内存限制,但有时地址空间是零散的,所以您确实得到700MB但较小的块"这样的回答感到满意.(这只是猜测,我仍然需要一个答案)

I'd be happy with an answer like "700MB is the virtual memory limit, but sometimes the address space is fragmented, so you DO get 700MB but in smaller chunks". (This is just speculation, I still need an answer)

有关虚拟内存的Apple文档页面上说:

The Apple doc page about virtual memory says:

尽管OS X支持后备存储,但iOS不支持.在iPhone中应用程序,磁盘上已经存在的只读数据(例如代码)页),只需从内存中删除,然后根据需要从磁盘中重新加载.

Although OS X supports a backing store, iOS does not. In iPhone applications, read-only data that is already on the disk (such as code pages) is simply removed from memory and reloaded from disk as needed.

这似乎证实了mmap应该适用于大于物理内存的块,但仍然无法解释为什么我达到了如此低的限制.

which seems to confirm that mmap should work for blocks larger than the physical memory but still doesn't explain why I'm hitting such a low limit.

更新

  • 此答案很有趣,但是500MB远远低于它提到的700MB限制.
  • 此讨论提到连续内存.那么内存碎片可能是一个真正的问题?
  • 我正在使用具有256MB物理内存的第四代iPod Touch.
  • 我的研究重点是,从文件中加载只读数据时,是否有比一直分配直到收到内存警告" 更好的管理内存的方法. mmap 似乎是解决此问题的好方法...
  • This answer is interesting, but 500MB is well below the 700MB limit it mentions.
  • This discussion mentions contiguous memory. So memory fragmentation could be a real issue?
  • I'm using iPod Touch 4th generation which has 256MB physical memory.
  • The point of my research is to see if there's a better way of managing memory when loading read-only data from files than "keep allocating until you get a memory warning". mmap seemed like a nice way to solve this...

更新2

我希望mmap可以与新的64位iOS版本完美配合.一旦接触到64位设备,即可进行测试.

I expect mmap to work perfectly with the new 64bit version of iOS. Will test once I get my hands on a 64bit device.

推荐答案

经过进一步调查并阅读了此

After further investigation and reading this excellent blog post by John Carmack, here are my conclusions:

  • 700 MB是iOS上的虚拟内存限制(从2012年开始,为32位iOS)
  • 它可能在单个块中可用,也可能不可用;这取决于设备状态和应用行为

因此,要可靠地映射700MB的文件数据,必须将其分成较小的块.

Therefore, to reliably mmap 700MB worth of file data it is necessary to break it into smaller chunks.

这篇关于为什么mmap在iOS上失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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