内存映射文件和低内存方案 [英] Memory-mapped files and low-memory scenarios

查看:113
本文介绍了内存映射文件和低内存方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在内存不足的情况下,iOS平台如何处理内存映射文件?在内存不足的情况下,我的意思是操作系统将UIApplicationDidReceiveMemoryWarningNotification通知发送给应用程序中的所有观察者.

How does the iOS platform handle memory-mapped files during low-memory scenarios? By low-memory scenarios, I mean when the OS sends the UIApplicationDidReceiveMemoryWarningNotification notification to all observers in the application.

使用 +[NSData dataWithContentsOfMappedFile:] ,该文档指出:

Our files are mapped into memory using +[NSData dataWithContentsOfMappedFile:], the documentation for which states:

映射文件使用虚拟内存技术来避免将文件页面复制到内存中,直到真正需要它们为止.

A mapped file uses virtual memory techniques to avoid copying pages of the file into memory until they are actually needed.

这是否意味着当不再使用页面时,操作系统也会取消映射页面?是否可以将页面标记为不再使用?如果这会更改方案,则此数据为只读.如果直接使用mmap()怎么办?这样会更好吗?

Does this mean that the OS will also unmap the pages when they're no longer in use? Is it possible to mark pages as being no longer in use? This data is read-only, if that changes the scenario. How about if we were to use mmap() directly? Would this be preferable?

推荐答案

内存映射文件一次将数据从磁盘复制到内存中.除非已使用mlock(2)将未使用的页面连接到物理内存中,否则可以自由交换掉未使用的页面.内存映射决定了要从磁盘复制到内存以及何时复制到操作系统的内容.

Memory-mapped files copy data from disk into memory a page at a time. Unused pages are free to be swapped out, the same as any other virtual memory, unless they have been wired into physical memory using mlock(2). Memory mapping leaves the determination of what to copy from disk to memory and when to the OS.

从Foundation级别拖放到BSD级别以使用mmap不会产生太大的变化,除了使必须与其他Foundation代码进行交互的代码更加尴尬之外.

Dropping from the Foundation level to the BSD level to use mmap is unlikely to make much difference, beyond making code that has to interface with other Foundation code somewhat more awkward.

这篇关于内存映射文件和低内存方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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