简体   繁体   English

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

[英]What are reasons for “Cannot allocate memory” except of exceeding address space and memory fragmentation?

The problem is that in a 32-bit application on Mac OS XI receive an error问题是在 Mac OS XI 上的 32 位应用程序中收到错误

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:参考错误代码在 sys/errno.h 中:

#define ENOMEM   12  /* Cannot allocate memory */

The memory allocation pattern is like this: memory 分配模式是这样的:

  1. First is allocated nearly 250MB of memory首先是分配了近250MB的memory
  2. Allocate 6 blocks of 32MB分配 6 个 32MB 的块
  3. Then 27 images each handled like this然后 27 个图像每个都像这样处理
    1. Allocate 16MB (image bitmap is loaded)分配16MB(图片bitmap已加载)
    2. Allocate 32MB, process it, free these 32MB分配 32MB,处理它,释放这 32MB
    3. Again allocate 32MB, process it, free these 32MB再次分配 32MB,处理它,释放这 32MB
    4. Free 16MB allocated in step 3.1步骤 3.1 中分配的免费 16MB
  4. Free 4 blocks allocated in step 2 (2 blocks are still used)释放步骤 2 中分配的 4 个块(仍使用 2 个块)
  5. Free 250MB block allocated in step 1步骤 1 中分配的 250MB 空闲块
  6. Allocate blocks of various size, total size doesn't exceed 250MB.分配各种大小的块,总大小不超过 250MB。 And here I receive the mentioned memory allocation error在这里我收到提到的 memory 分配错误

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.我已经检查过这些 memory 块都没有泄漏,所以我猜想在任何给定时间使用的 memory 都保持在 1GB 以下,应该可以在 32 位系统上访问。

The second guess was memory fragmentation.第二个猜测是 memory 碎片。 But I've checked that all block in step 3 reuse same addresses.但是我已经检查了步骤 3 中的所有块都重用了相同的地址。 So I touch less than 1GB of memory - memory fragmentation should not be an issue.所以我接触不到 1GB 的 memory - memory 碎片应该不是问题。

Now I am completely lost what can be a reason for not allocating memory.现在我完全失去了不分配 memory 的原因。 Also everything works OK when I process less than 27 images.当我处理少于 27 张图像时,一切正常。 Here is part of heap command result before step 6 for 26 images:以下是第 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

And for 27 images:对于 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

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?或者可能我在排除上述原因时犯了错误,那么我该如何再次检查它们?

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而不是使用heap命令。 vmmap revealed that most of the memory is used by images mapped into the memory. vmmap显示大部分 memory 被映射到 memory 的图像使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM