你曾经使用过 NSZoneMalloc() 而不是 malloc() 吗?

发布于 2024-07-14 02:42:44 字数 607 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最初的梦 2024-07-21 02:42:44

我为 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.

月隐月明月朦胧 2024-07-21 02:42:44

您的整个问题是绝对正确的,但实际上,没有人真正使用区域。 正如您链接到的页面所说:

在大多数情况下,使用默认区域比创建单独的区域更快、更高效。

创建自己的区域的好处是:

如果在尝试访问其中一个对象时发生页面错误,加载该页面会引入所有相关对象,这可以显着减少未来页面错误的数量。

如果发生页面错误,则意味着系统最近对内容进行了分页,因此速度很慢,并且您的应用程序不负责任,或者解决方案在于您的应用程序在第一次分配了太多内存的部分地方。

所以,基本上,问题是“你能否证明你确实需要创建自己的区域来解决性能问题或让你的应用程序快速运行”,答案是“不”。

You're absolutely right in your entire question, but in practice, nobody really uses zones. As the page you link to puts it:

In most circumstances, using the default zone is faster and more efficient than creating a separate zone.

The benefit of making your own zone is:

If a page fault occurs when trying to access one of the objects, loading the page brings in all of the related objects, which could significantly reduce the number of future page faults.

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”.

梦魇绽荼蘼 2024-07-21 02:42:44

如果你发现自己这样做,那么你的操作水平可能低于你真正应该达到的水平。 子系统几乎忽略它们; 任何对 +alloc 等的调用都会让您获得默认区域中的对象。 您只需要了解 mallocNSAllocateCollectable 即可。

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 and NSAllocateCollectable are all you need to know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文