[英]NSMutableDictionary cocoa memory leak
Is there any memory leak in below code? 下面的代码是否有任何内存泄漏? I guess NO, but for me below code allocates memory but not releasing it.
我猜不是,但是对我来说,下面的代码分配了内存,但没有释放它。
Any help is appreciated 任何帮助表示赞赏
@autoreleasepool {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i = 0; i < 300000; i++) {
@autoreleasepool {
[dict setObject:[NSNumber numberWithInt:i] forKey:[NSNumber numberWithInt:i]];
}
}
[dict removeAllObjects];
}
I don't know if this affects your issue, but it is worth knowing... 我不知道这是否会影响您的问题,但值得知道...
NSNumber does some fancy things behind the scenes, especially for the integers 0 - 12. It doesn't always release them, since it assumes that it is highly likely that it will need to reuse them in the future. NSNumber在幕后做了一些特别的事情,特别是对于0到12的整数。它并不总是释放它们,因为它假定将来很有可能需要重用它们。 They are considered to be used commonly enough to not want to retain / delete them all of the time.
人们认为它们足够常用,以至于不想一直保留/删除它们。
See this discussion . 看到这个讨论 。
This might account for a little bit of memory not getting released, but it won't account for a huge amount. 这可能会占用少量内存,但不会释放大量内存。
It would be interesting to put your above code in a for loop, and see if calling it many times increases memory, or if the amount of memory used stays flat. 将上面的代码放入for循环中,看看是否多次调用它会增加内存,或者使用的内存量是否保持不变,将是很有趣的。
for (int i = 0; i < 100; i++) {
@autoreleasepool {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i = 0; i < 300000; i++) {
@autoreleasepool {
[dict setObject:[NSNumber numberWithInt:i] forKey:[NSNumber numberWithInt:i]];
}
}
[dict removeAllObjects];
}
}
If it stays flat, I wouldn't worry too much about it. 如果它保持平坦,我不会对此太担心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.