Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

When I use The Class of NSMutableArray , first I alloc a NSMutableArray,Then Add Five Object to the NSMutableArray, last Remove All Object from the Array .the Memory Used can't Release.

When I use The Class of NSMutableArray , first I alloc a NSMutableArray,Then Add Five Object to the NSMutableArray, last Remove All Object from the Array .the Memory Used can't Release.



The Follow is My Code for the Question:


NSMutableArray * _test = [[NSMutableArray alloc] init] ;

NSString * str = [[NSString alloc]initWithFormat:@"Test12312313"];

NSLog(@"retainCount0:%d",[str retainCount]);


[_test addObject:str];

[str release];

NSLog(@"retainCount0:%d",[str retainCount]);

double duse = [self usedMemory];

NSLog(@"Test0:%.10f",duse);

int n = 0;

{

while (n < 10000 ) {

n++;

NSString * str = [[NSString alloc]initWithFormat:@"Test12312313"];

[_test addObject:str];

str = nil;

duse = [self usedMemory];

NSLog(@"Test1:%.10f",duse);

}

[_test removeAllObjects];

_test = nil;

str = nil;

duse = [self usedMemory];

NSLog(@"Test2:%.10f",duse);

}

duse = [self usedMemory];

NSLog(@"Test3:%.10f",duse);

Posted on Oct 15, 2013 5:56 PM

Reply
1 reply

Oct 15, 2013 7:39 PM in response to gzl_supermap

In general, retainCount is widely considered useless. Best to simply ignore that method.


I'd also encourage using automatic reference counting (ARC) here, and not manual retain-release. Here's some related llvm documentation with ARC details and here's a quick overview. Some example code here.


If you want to check for leaks in your code, it's also generally best to use Instruments.

When I use The Class of NSMutableArray , first I alloc a NSMutableArray,Then Add Five Object to the NSMutableArray, last Remove All Object from the Array .the Memory Used can't Release.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.