Class memory size in Objective-C

I am interested in knowing the size of memory by allocating some Objective-C objects.

For example:

Is [NSString stringWithString:@"2"] larger than [NSNumber numberWithInt:2] or not?

And how much more [NSNumber numberWithInt:2] than int num=2 ?

Is there any documentation from Apple on this? I think this information is very important for memory optimization.

+7
source share
4 answers

Exact documentation is not available as far as I know. NSString and (IIRC) NSNumber are implemented as class clusters, i.e. When you request a new object, you can actually get the object of some undocumented subclass.

It also means that everything can change without warning when your program runs on a different version of the OS, so do not rely on exact numbers.

Now let's try a rough estimate. As many as 4 bytes on all modern Apple platforms. Pointers have 4 bytes on iOS.

Objects are allocated on the heap; at the lowest level, heap allocation is done using malloc . I assume that the iOS malloc implementation is derived from the one used on Mac OS. Check here for details: http://cocoawithlove.com/2010/05/look-at-how-malloc-works-on-mac.html

The most important point is that the distribution quantum for small objects is 16 bytes, i.e. small objects will use a multiple of 16 bytes.

Each Objective-C object contains a pointer to its class.

So for an NSNumber containing an int, I would evaluate 4 bytes for your pointer plus 16 bytes for the object (consisting of a 4-byte class pointer and, I suppose, a four-byte int plus 8 bytes of lost space).

There are different specific subclasses for NSString for different situations. String literal @"2" will point to a statically allocated string literal, a string created at runtime is likely to have a different representation. In general, I would suggest that 4 bytes (your pointer) + 16 bytes (NSString object) + the number of characters * 2 (sizeof (unichar)) is rounded to a multiple of 16.

To summarize, I NSNumbers that NSNumbers require about five times as much memory as int s. I also estimate that the same number represented as NSString takes about 10 times as much memory as int .

Also note that allocating Objective-C objects is much slower than defining a local variable of type int. However, you must also remember that this often does not matter and that premature optimization is the root of all evil.

+14
source

There is no such thing as NSString or NSNumber , their logic is actually implemented in several different classes, each of which has more or less its own amount of memory. Therefore, it is impossible to say how much the class weighs before the runtime (however, you can ask ObjC about this runtime). This suggests that, most likely, [NSNumber numberWithInt:2] will return one instance of NSNumber (each integer up to 12 cached), so you really are not adding anything to the memory area.

And besides everything else, the path to optimizing your memory is the wrong way! If an object weighs 64 bytes more than another, so what? 64 bytes is nothing, even on the iPhone! You will have big memory problems where you can actually optimize, for example. lazy loading of your things or something else. But these problems are detected during testing using tools, and not forward when you write your code! (You would be surprised how often a simple β€œoptimization” made before the test was launched has no effect or even has a worse impact on performance!)

+4
source

In the case of NSNumber and NSString

 NSLog(@"size was %ld", class_getInstanceSize([NSNumber class])); NSLog(@"size was %ld", class_getInstanceSize([NSString class])); 

gives 8 and 8 on my Mac (which has 64 bit pointers).

The documentation is here: http://developer.apple.com/library/ios/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/uid/TP40001418-CH1g-SW38

+2
source

Take a look at Instruments . These tools should answer your questions.

+1
source

All Articles