我的观点是,当我们创建对象并将其设置为nil时,这个对象将被释放。但是我尝试在NSMutableDictionary中插入大量数据,然后为每个对象设置为空。内存仍然存在,并且没有减少。请参考我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableDictionary*frameAnotationLeft=[[NSMutableDictionary alloc] init];;
// Do any additional setup after loading the view, typically from a nib.
NSMutableDictionary * testDict = [[NSMutableDictionary alloc] init];
frameAnotationLeft = [[NSMutableDictionary alloc] init];
for ( int j=0; j<1000; j++) {
for (int i= 0; i<5000; i++) {
NSString* __weak test = @"dule.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.";
NSMutableArray * arrayTest = [[NSMutableArray alloc] init];
[arrayTest addObject:test];
test = nil;
[testDict setObject:arrayTest forKey:[NSString stringWithFormat:@"%d",i]];
arrayTest = nil;
}
[frameAnotationLeft setObject:testDict forKey:[NSString stringWithFormat:@"%d",j]];
}
testDict = nil;
// Begin to release memory
for (NSString* key in frameAnotationLeft) {
NSMutableDictionary *testDict2 = (NSMutableDictionary*)[frameAnotationLeft objectForKey:key];
for (NSString* key2 in testDict2) {
NSMutableArray * arrayTest = (NSMutableArray *)[testDict2 objectForKey:key2];
NSString* test = [arrayTest objectAtIndex:0];
[arrayTest removeAllObjects];
test = nil;
arrayTest = nil;
}
[testDict2 removeAllObjects];
testDict2 = nil;
}
[frameAnotationLeft removeAllObjects];
frameAnotationLeft = nil;
}当我运行它的时候内存是218MB。而且它并没有减少。有没有人能帮我,给我解决方案?谢谢你,谢谢你
发布于 2015-09-14 00:55:00
如果您使用的是ARC,则不需要将局部变量设置为nil,并且不会产生任何效果。局部变量完全由内存管理。正如您想象的那样,将变量设置为nil并不会导致内存本身被清除;它会减少对象的保留计数,但仅此而已。如果对象的保留计数已经降到零,那么内存可能会在将来的某个时间被回收,但您无法控制何时会发生这种情况。因此,你的nil设置什么也做不了,因为同样的事情也是由ARC为你做的。
如果您担心在紧密循环期间(如此处)二次自动释放对象的累积,只需将循环的内部包装在@autoreleasepool块中。
https://stackoverflow.com/questions/32551942
复制相似问题