Xcode malloc with memset did not cause memory increase on Release

97 Views Asked by At

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

1

There are 1 best solutions below

1
gnasher729 On BEST ANSWER

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?