2015-09-13 76 views
0

点我的看法,当我们创建对象,然后将其设置=零,这个对象将是释放。但我试图插入大量的数据到NSMutableDictionary,然后我为每个对象设置NIL。记忆仍然在那里,并没有减少。请参阅我的代码:内存释放目标C

- (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; 

} 

内存当我运行它是218 MB。并没有减少。有人可以帮助我,给我解决方案?谢谢如果你正在使用ARC,局部变量设置为nil没有必要如此muuch

回答

0

,并没有任何效果。局部变量完全由内存管理。并设置一个变量nil确实不是,如你所想,导致内存本身被清除;它减少了对象的保留数量,但仅此而已。如果对象的保留计数已降至零,则内存可能会在未来某个时间回收,但您无法控制何时会发生这种情况。因此,你的nil设置什么都不做,因为ARC也在为你完成同样的事情。

如果您担心在紧密循环中累积次级自动释放对象(如此处),只需将循环内部包装在@autoreleasepool块中即可。

+0

另外,不要在模拟器中测试内存管理;它不像真实设备那样工作。如果您想知道实际发生的情况,只能在设备上进行测试。 – matt

+0

谢谢,我在真实设备上测试过(iphone和ipad)..我很迷惑它。我也试过了@autoreleasepool,但它不起作用 –

+0

但是,正如我所说的,如果内存不能被回收,那只会有问题。我不认为你知道这一点。如果您想知道不需要的对象是否持续存在,请使用“仪器分配”工具。如果您想知道是否有_leak_,请使用“仪器泄漏”工具。如果没有,那么不要担心,继续前进。 – matt