我開發可可程序,使用的內存可能的最小量的希望。要檢查這一點,我一直在使用活動監視器。我的程序有NSString
相當大NSMutableArray
含有的情況下,當我鬆開陣列,我沒有得到我所有的記憶回來了。以下示例演示了我的問題。Objective-C的內存不被釋放,大型排列
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:10];
NSLog(@"The array has no elements!"); // Activity monitor says 1.5 megs
[NSThread sleepForTimeInterval:10];
NSUInteger i;
for(i = 0;i < 1000000;i++)
{
NSString *str = [[NSString alloc] initWithFormat:@"Number=%d, again it is: %d, one more time for added length: %d", i, i, i];
[array addObject:str];
[str release];
}
NSLog(@"We have used lots of memory!"); // Activity Monitor says 108 megs
[NSThread sleepForTimeInterval:5];
[array release];
NSLog(@"We have released the array!"); // Activity Monitor says 19 megs
[NSThread sleepForTimeInterval:5];
[pool drain];
NSLog(@"We have drained the pool!"); // Activity Monitor still says 19 megs
[NSThread sleepForTimeInterval:5];
return 0;
}
我加入sleepForTimeInterval
電話,所以我可以看到和注意到活動監視器中列出的內存使用情況。在每次停頓時,我都列出了我應該使用的內存量。我幾乎可以肯定,我的問題源於保留/釋放公約中的誤解。有沒有人看到我要去哪裏錯了?
AM柱你在看什麼? – ergosys 2010-01-13 23:24:34
我正在查看Real Memory列。 – 2010-01-14 00:12:58