ios - Objective-C Memory Issue with NSArray literal using NSNumber object -


i have started out objective-c , trying understand memory management. came across peculiar problem yesterday causing memory leak of on 150mb! traced down piece of code creating nsarray literal of nsnumbers. managed solve issue using nsmutablearray (using addobject), haven't been able grasp concept why 1 method works , other doesn't. love better understanding of memory management explain concept behind can avoid such mistakes in future.

to better illustrate question, let me provide code snippets. instance, have function creates nsarray of few nsnumbers , returns , gets called many times:

-(nsarray *)getnsnumberarray {     float num1 = 23.56;     float num2 = 75.34;     float num3 = 223.56;      nsarray *numarray = @[[nsnumber numberwithfloat:num1], [nsnumber numberwithfloat:num2], [nsnumber numberwithfloat:num3]];     return numarray; }  -(void)causememoryleak {     (int = 0; < 2000000; i++)     {         nsarray *recieverarray = [self getnsnumberarray];     } } 

this results in memory being occupied more 200mb on iphone6. if change function be:

-(nsarray *)getnsnumberarray {     float num1 = 23.56;     float num2 = 75.34;     float num3 = 223.56;      nsmutablearray *nummutablearray = [[nsmutablearray alloc] init];     nsnumber *nsnumber1 = [nsnumber numberwithfloat:num1];     nsnumber *nsnumber2 = [nsnumber numberwithfloat:num2];     nsnumber *nsnumber3 = [nsnumber numberwithfloat:num3];     [nummutablearray addobject:nsnumber1];     [nummutablearray addobject:nsnumber2];     [nummutablearray addobject:nsnumber3];     return nummutablearray; }  -(void)causememoryleak {     (int = 0; < 2000000; i++)     {         nsarray *recieverarray = [self getnsnumberarray];     } } 

this not cause memory issues.

maybe missing obvious, still cant figure out reason behind this. appreciate on this. there better ways of doing it, , such answers welcome looking explanation behind it.

many in advance!

attaching links screenshots showing memory allocation on device (iphone 6) (i can not attach images here of now, have provide links)

approach 1 memory allocation (memory retained , not freed up): https://drive.google.com/open?id=0b-a9wjsbuil4btr5rtvuawpqyke&authuser=0

approach 2 memory allocation (memory freed , there no surge in memory allocation well): https://drive.google.com/open?id=0b-a9wjsbuil4qzqzbgyyqzzddw8&authuser=0

i tried code because didn't found reason memory leak , there no memory leak.

when use regular array, device runs code, , releases memory @ end. causing heap alloc 250 mb, , release them @ end.

if use mutable array, device releases previous array before creating new one, there no point many allocation.

i guess different may caused run time complexity of called function in loop.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -