Home > DeveloperSection > Forums > Convenience Vs init method (memory usage)
Jennifer Morgan
Jennifer Morgan

Total Post:13

Points:95
Posted on    October-24-2015 12:10 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 482  View(s)
Rate this:

I tried to create UIImageView object using Convenience and init method, after when I try to go back and check the memory usage that are increases after creation of both methods, the memory usage under the convenience method takes 10MB to 7MB and init method takes from 10MB to 4MB immediately both examples are here:
Convenience Method:

imageView.image = [UIImage imageWithData:[downloads dataAtIndex:0]];

init Method:

UIImage *aImage = [[UIImage alloc] initWithData:[downloads dataAtIndex:0]];
imageView.image = aImage;
[aImage release];

I also waited and interacted with the app to give time on releasing on the convenience method's autorelease. But it didn't drop much.
Other than the autorelease vs release, what else contributed the difference?



Tarun Kumar

Total Post:213

Points:1635
Supported
Posted on    October-25-2015 11:16 PM

The only difference that could be causing this is that imageWithData: doesn't use the system image cache, while initWithData: does. So maybe the image that's made with the initializer can release it's image data when it receives a memory warning since it can go back to the system cache, while the one that's created by the convenience method can't.
It is advisable to wrap memory intensive operations inside of an NSAutoreleasePool block if you will be making heavy use of them.

Don't want to miss updates? Please click the below button!

Follow MindStick