1

I have tested the code in Xcode 10.3

- (void)loopObjectMalloc {
    while (1) {
        NSObject *obj = [[NSObject alloc] init];
    }
}

I expect the OOM happened, but memory not increased. Is the alloc function not memset to the physical memory ?

Kylin.Zhang
  • 103
  • 7
  • 2
    obj will be released at the end of each while loop. Of course, memory won’t be increased. – trungduc Sep 09 '19 at 08:33
  • @trungduc but the runloop keeps working, so when did release function happened? – Kylin.Zhang Sep 09 '19 at 08:47
  • As @trungduc has stated, by default Automatic Reference Counting (ARC) is turned on. Turn it off to test. Update: and your last comment sounds like you are talking about autorelease functionality. – trojanfoe Sep 09 '19 at 08:47
  • @Kylin.Zhang I think you are misunderstanding between the end of the loop and the end of function. – trungduc Sep 09 '19 at 08:56
  • 1
    @trungduc yes I am confused about it, is there a autoreleasepool in the while loop? – Kylin.Zhang Sep 09 '19 at 09:19
  • disable ARC for file.m -fno-objc-arc https://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project – Roman Solodyashkin Sep 09 '19 at 10:15

2 Answers2

2

By default Automatic Reference Counting (ARC) is turned on. So obj will be released at the end of each loop and make memory not be increased.

obj doesn't need to wait for every loops finished.

- (void)loopObjectMalloc {
    while (1) {
        // At the beginning of each loop, `obj` is created.
        NSObject *obj = [[NSObject alloc] init];
        // End of the loop, obj is released due to out of scope.
    }
    // End of function.
}

There is no need autorelease pool to release your object. release will be inserted to your code at compile time automatically.

retain, release, retainCount, autorelease or dealloc cannot be sent to objects. Instead, the compiler inserts these messages at compile time automatically, including [super dealloc] when dealloc is overridden.

https://en.wikipedia.org/wiki/Automatic_Reference_Counting#Objective-C

Note: If you want to see OOM, turn off ARC.

trungduc
  • 11,926
  • 4
  • 31
  • 55
  • You should mention that this will only happen if ARC is turned on. – trojanfoe Sep 09 '19 at 09:12
  • 1
    I think there do not have an autoreleasepool in while loop, I still have no idea why release happened – Kylin.Zhang Sep 09 '19 at 10:05
  • 1
    @Kylin.Zhang I don't think you understand what an "auto released" object is. Your question and this answer has nothing to do with it. Objective-C requires you to `retain` and `release` objects manually, unless you use ARC (now the default) and that is the functionality in-play in your question. The ARC system sees the object going out-of-scope and inserts a `release` call to the object. Hence it's managing the memory for you. – trojanfoe Sep 09 '19 at 10:23
  • @trojanfoe I am understanding what you mean, thank you very much – Kylin.Zhang Sep 09 '19 at 11:17
0

You can use use bridging functions to take ownership from ARC:

- (void)loopObjectMalloc {
    while (1) {
        CFTypeRef obj = CFBridgingRetain([[NSObject alloc] init]);
    }
}
Cy-4AH
  • 4,370
  • 2
  • 15
  • 22