你曾经使用过 NSZoneMalloc() 而不是 malloc() 吗?
Cocoa 提供了页面对齐的内存区域,它称之为内存区域< /a>,并提供一些以区域作为参数的内存管理函数。
假设您需要分配一块内存(不是为对象,而是为任意数据)。 如果调用malloc(size)
,缓冲区将始终分配在默认区域中。 但是,有人可能使用 allocWithZone:
在默认区域之外的另一个区域中分配您的对象。 在这种情况下,最好使用 NSZoneMalloc([self zone], size) ,它将缓冲区和所属对象保留在同一内存区域中。
你遵循这种做法吗? 您曾经使用过内存区域吗?
更新:我认为 Stack Overflow 上有一种倾向,通过有关过早优化的讲座来回答有关低级主题的问题。 我知道 1993 年 NeXT 硬件上的区域可能比今天更重要,而且 Google 搜索很清楚地表明几乎没有人关心它们。 无论如何,我想看看是否有人可以描述一个使用内存区域的项目。
Cocoa provides for page-aligned memory areas that it calls Memory Zones, and provides a few memory management functions that take a zone as an argument.
Let's assume you need to allocate a block of memory (not for an object, but for arbitrary data). If you call malloc(size)
, the buffer will always be allocated in the default zone. However, somebody may have used allocWithZone:
to allocate your object in another zone besides the default. In that case, it would seem better to use NSZoneMalloc([self zone], size)
, which keeps your buffer and owning object in the same area of memory.
Do you follow this practice? Have you ever made use of memory zones?
Update: I think there is a tendency on Stack Overflow to respond to questions about low-level topics with a lecture about premature optimization. I understand that zones probably mattered more in 1993 on NeXT hardware than they do today, and a Google search makes it pretty clear that virtually nobody is concerned with them. I am asking anyway, to see if somebody could describe a project where they made use of memory zones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为 Linux 上的 NeXTStep、GNUstep 和 Mac OS X 上的 Cocoa 编写了软件,并且从未需要使用自定义内存区域。 表明它是对软件的良好改进的条件要么从未出现过,要么从未被检测到具有重要意义。
I've written software for NeXTStep, GNUstep on Linux and Cocoa on Mac OS X, and have never needed to use custom memory zones. The condition which would suggest it as a good improvement to the software has either never arisen, or never been detected as significant.
您的整个问题是绝对正确的,但实际上,没有人真正使用区域。 正如您链接到的页面所说:
创建自己的区域的好处是:
如果发生页面错误,则意味着系统最近对内容进行了分页,因此速度很慢,并且您的应用程序不负责任,或者解决方案在于您的应用程序在第一次分配了太多内存的部分地方。
所以,基本上,问题是“你能否证明你确实需要创建自己的区域来解决性能问题或让你的应用程序快速运行”,答案是“不”。
You're absolutely right in your entire question, but in practice, nobody really uses zones. As the page you link to puts it:
The benefit of making your own zone is:
If a page fault occurs, that means that the system was recently paging things out and is therefore slow anyway, and that either your app is not responsible or the solution is in the part of your app that allocated too much memory at once in the first place.
So, basically, the question is “can you prove that you really do need to create your own zone to fix a performance problem or make your app wicked fast”, and the answer is “no”.
如果你发现自己这样做,那么你的操作水平可能低于你真正应该达到的水平。 子系统几乎忽略它们; 任何对
+alloc
等的调用都会让您获得默认区域中的对象。 您只需要了解malloc
和NSAllocateCollectable
即可。If you find yourself doing this, you're probably operating at a lower level than you really ought to be. The subsystem pretty much ignores them; any calls to
+alloc
or such will get you objects in the default zone.malloc
andNSAllocateCollectable
are all you need to know.