2015-09-13 111 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