Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Memory consumption for data type allocation

It may be a basic question but just curious to know the answer. How much memory will be occupied when we are creating each variable like int, NSString, NSDictionary, NSData etc. in our iOS Obj C program. I hope, it will be similar to C program based on the size of the data type (or) differently ?

Thank you!

like image 784
Stella Avatar asked Feb 15 '23 19:02

Stella


1 Answers

In short, you can't. At least not in a useful fashion.

Just about every class will have any number of allocations that are not directly credited to the instance's allocation directly. As well, many classes -- NSDictionary, NSArray, NSString -- are actually a part of a class cluster and, thus, what is actually allocated is a subclass. Finally, for the various collection and data classes, the size of the associated allocations are going to vary wildly based on their contents. Some classes -- UIImage, NSData, etc -- may contain MBs of data that isn't actually represented by a heap allocation in that they are mapping data from the filesystem.

Or, to summarize, class_getInstanceSize() is useless.

Instead, you need to focus on the memory usage of your app as a systemic characteristic of its operating behavior. The Allocations Instrument can do a good job of measuring memory usage and, more importantly, help you identify what is responsible for consumption therein.

like image 94
bbum Avatar answered Feb 26 '23 16:02

bbum