带有 memset 的 Xcode malloc 不会导致 Release 上的内存增加

问题描述 投票:0回答:1

我想通过下面的代码手动创建一个oom:

[NSTimer scheduledTimerWithTimeInterval:0.1 repeats:true block:^(NSTimer * _Nonnull timer) {
    void *bytes = malloc(1024*1024*50);
    memset(bytes, 1, 1024*1024*50);
}];

但是内存没有增加,我改成下面的代码:

void **array = malloc(UINT32_MAX*sizeof(void *));
__block int64_t i = 0;
[NSTimer scheduledTimerWithTimeInterval:0.1 repeats:true block:^(NSTimer * _Nonnull timer) {
    void *bytes = malloc(1024*1024*50);
    memset(bytes, 1, 1024*1024*50);
    array[i] = bytes;
    i++;
}];

然后 oom

所以我的问题是为什么第一个 malloc 和 memset 没有增加实际内存使用。 另一个信息是第一个代码在调试时导致 oom,但在发布时无用。 谁能对此给出一些解释(不是明显的怀疑)

ios malloc out-of-memory memset
1个回答
0
投票

你有一个优化编译器。如果编译器可以证明 malloc() 调用毫无意义,就像这里一样,那么它可以删除该调用。这很可能发生了什么。

而且我在您的代码中找不到任何“发布”。那么你能修复标题,或者添加缺失的代码吗?

© www.soinside.com 2019 - 2024. All rights reserved.