John
John

Reputation: 65

Xcode malloc with memset did not cause memory increase on Release

I want to manually create a oom by code below:

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

But memory did not increase, and I change to code below:

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++;
}];

And get oom

So my problem is why the first malloc and memset did not increase real memory useage. Another infomation is that the first code cause oom at Debug, but useless on Release. Can anyone give some explanation(not obvious suspicion) about this

Upvotes: 0

Views: 122

Answers (1)

gnasher729
gnasher729

Reputation: 52538

You have an optimising compiler. If a compiler can prove that a malloc() call was pointless, like here, then it can remove the call. That's most likely what happened.

And I cannot find any "Release" in your code. So can you fix the title, or add the missing code?

Upvotes: 0

Related Questions