“无法分配内存”的原因是什么?除了超出地址空间和内存碎片之外?

发布于 2024-11-26 07:00:18 字数 2248 浏览 1 评论 0原文

问题是,在 Mac OS X 上的 32 位应用程序中,收到错误

malloc: *** mmap(size=49721344) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

参考错误代码位于 sys/errno.h 中:

#define ENOMEM   12  /* Cannot allocate memory */

内存分配模式如下:

  1. 首先分配近 250MB 的内存
  2. 分配 6 个 32MB 的块
  3. 然后27张图像每张都这样处理
    1. 分配 16MB(图像位图已加载)
    2. 分配 32MB,处理它,释放这 32MB
    3. 再次分配32MB,处理它,释放这32MB
    4. 第 3.1 步中分配的免费 16MB
  4. 释放步骤 2 中分配的 4 个块(仍使用 2 个块) )
  5. 步骤 1 中分配的空闲 250MB 块
  6. 分配各种大小的块,总大小不超过 250MB。在这里,我收到了提到的内存分配错误,

我已经检查过这些内存块都没有泄漏,所以我猜想在任何给定时间使用的内存都保持在 1GB 以下,这应该可以在 32 位系统上访问。

第二个猜测是内存碎片。但我已经检查过步骤 3 中的所有块都重复使用相同的地址。所以我接触的内存不到 1GB - 内存碎片应该不是问题。

现在我完全不知道不分配内存的原因是什么。当我处理少于 27 个图像时,一切正常。以下是步骤 6 之前的 26 个图像的堆命令结果的一部分:

Process 1230: 4 zones
Zone DefaultMallocZone_0x273000: Overall size: 175627KB; 29620 nodes malloced for 68559KB (39% of capacity); largest unused: [0x6f800000-8191KB]
Zone DispatchContinuations_0x292000: Overall size: 4096KB; 1 nodes malloced for 1KB (0% of capacity); largest unused: [0x2600000-1023KB]
Zone QuartzCore_0x884400: Overall size: 232KB; 7039 nodes malloced for 132KB (56% of capacity); largest unused: [0x3778ca0-8KB]
Zone DefaultPurgeableMallocZone_0x27f2000: Overall size: 4KB; 0 nodes malloced for 0KB (0% of capacity); largest unused: [0x3723000-4KB]
All zones: 36660 nodes malloced - 68691KB

对于 27 个图像:

Process 1212: 4 zones
Zone DefaultMallocZone_0x273000: Overall size: 167435KB; 30301 nodes malloced for 68681KB (41% of capacity); largest unused: [0x6ea51000-32372KB]
Zone DispatchContinuations_0x292000: Overall size: 4096KB; 1 nodes malloced for 1KB (0% of capacity); largest unused: [0x500000-1023KB]
Zone QuartzCore_0x106b000: Overall size: 192KB; 5331 nodes malloced for 101KB (52% of capacity); largest unused: [0x37f2f98-8KB]
Zone DefaultPurgeableMallocZone_0x30f8000: Overall size: 4KB; 0 nodes malloced for 0KB (0% of capacity); largest unused: [0x368f000-4KB]
All zones: 35633 nodes malloced - 68782KB

那么“无法分配内存”的其他原因是什么以及如何诊断它们?或者我可能犯了一个错误,排除了上述原因,那么我该如何再次检查它们呢?

The problem is that in a 32-bit application on Mac OS X I receive an error

malloc: *** mmap(size=49721344) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

For the reference error code is in sys/errno.h:

#define ENOMEM   12  /* Cannot allocate memory */

The memory allocation pattern is like this:

  1. First is allocated nearly 250MB of memory
  2. Allocate 6 blocks of 32MB
  3. Then 27 images each handled like this
    1. Allocate 16MB (image bitmap is loaded)
    2. Allocate 32MB, process it, free these 32MB
    3. Again allocate 32MB, process it, free these 32MB
    4. Free 16MB allocated in step 3.1
  4. Free 4 blocks allocated in step 2 (2 blocks are still used)
  5. Free 250MB block allocated in step 1
  6. Allocate blocks of various size, total size doesn't exceed 250MB. And here I receive the mentioned memory allocation error

I've checked that none of these memory blocks is leaked, so I guess used memory at any given time stays below 1GB, which should be accessible on 32-bit system.

The second guess was memory fragmentation. But I've checked that all block in step 3 reuse same addresses. So I touch less than 1GB of memory - memory fragmentation should not be an issue.

Now I am completely lost what can be a reason for not allocating memory. Also everything works OK when I process less than 27 images. Here is part of heap command result before step 6 for 26 images:

Process 1230: 4 zones
Zone DefaultMallocZone_0x273000: Overall size: 175627KB; 29620 nodes malloced for 68559KB (39% of capacity); largest unused: [0x6f800000-8191KB]
Zone DispatchContinuations_0x292000: Overall size: 4096KB; 1 nodes malloced for 1KB (0% of capacity); largest unused: [0x2600000-1023KB]
Zone QuartzCore_0x884400: Overall size: 232KB; 7039 nodes malloced for 132KB (56% of capacity); largest unused: [0x3778ca0-8KB]
Zone DefaultPurgeableMallocZone_0x27f2000: Overall size: 4KB; 0 nodes malloced for 0KB (0% of capacity); largest unused: [0x3723000-4KB]
All zones: 36660 nodes malloced - 68691KB

And for 27 images:

Process 1212: 4 zones
Zone DefaultMallocZone_0x273000: Overall size: 167435KB; 30301 nodes malloced for 68681KB (41% of capacity); largest unused: [0x6ea51000-32372KB]
Zone DispatchContinuations_0x292000: Overall size: 4096KB; 1 nodes malloced for 1KB (0% of capacity); largest unused: [0x500000-1023KB]
Zone QuartzCore_0x106b000: Overall size: 192KB; 5331 nodes malloced for 101KB (52% of capacity); largest unused: [0x37f2f98-8KB]
Zone DefaultPurgeableMallocZone_0x30f8000: Overall size: 4KB; 0 nodes malloced for 0KB (0% of capacity); largest unused: [0x368f000-4KB]
All zones: 35633 nodes malloced - 68782KB

So what are other reasons for "Cannot allocate memory" and how can I diagnose them? Or probably I made a mistake ruling out mentioned reasons, then how can I check them again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无所谓啦 2024-12-03 07:00:18

结果我在检查地址空间是否耗尽时犯了一个错误。我应该使用 vmmap,而不是使用 heap 命令。 vmmap 显示大部分内存被映射到内存的图像使用。

Turned out I've made a mistake checking that address space is not exhausted. Instead of using heap command I should have used vmmap. vmmap revealed that most of the memory is used by images mapped into the memory.

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