Ethansong Ethansong - 2 months ago 6
Objective-C Question

ios NSNumber in NSMutableArray not releasing

my program works fine but the memory occupation is expending rapidly, i've debug for a long time and found such a problem:

if (some_condition)
NSMutableArray *fftArray256 = [NSMutableArray array];
NSMutableArray *fftArray = [NSMutableArray array];

[ESUtils changeToFFTData:pDatas outData:fftArray];//some data will be inserted in the fftArray.
for(NSNumber *n in fftArray)
double d = [n doubleValue];
d *= 256;//change the scale
[fftArray256 addObject:[NSNumber numberWithDouble:d]];
} = [self getDegree:fftArray256];

[fftArray256 removeAllObjects];
[fftArray removeAllObjects];

if i replace

[fftArray256 addObject:[NSNumber numberWithDouble:d]];


[fftArray256 addObject:@"1"];

everything will be fine, memory will grow before the last two lines, but after that all memory will be released.

if not changing like above the memory occupation will grow after leaving this block, it will repeat endlessly till the program fails. the removeAllObjects method could release part memory but remains a major part in memory, even after leaving this block.

so what's the problem with the [NSNumber numberWithDouble:d]? how can i release them totally before i leave this block?

i know it's not making sense, or I won't ask...


@"1" - is literal string, as i know it always the same instance

[NSNumber numberWithDouble:d] - it will create always new instance

To release memory try to wrap you code in autoreleasepool

@autoreleasepool {
    NSMutableArray *fftArray256 = [NSMutableArray array];
    [fftArray removeAllObjects];