Exact documentation is not available, to the best of my knowledge.
NSString and (IIRC) NSNumber are implemented as class clusters, i.e. when you ask for a new object, you might actually get an object of some undocumented subclass.
It also means that things may change without warning when your program runs on a different OS version, so don't rely on exact numbers.
Now, let's try a rough estimate. Integers are 4 bytes on all current Apple platforms.
Pointers are 4 bytes on iOS.
Objects are allocated on the heap; at the lowest level, heap allocation is done by malloc
.
I will assume that iOS's malloc implementation is derived from the one used on Mac OS - Look here for some details: http://cocoawithlove.com/2010/05/look-at-how-malloc-works-on-mac.html
The most important point is that the allocation quantum for small objects is 16 bytes, i.e. small objects will use up a multiple of 16 bytes.
Every Objective-C object contains a pointer to its class.
So, for a NSNumber containing an int I'd estimate 4 bytes for your pointer, plus 16 bytes for the object (consisting of a 4-byte class pointer and - I guess - a four-byte int, plus 8 bytes of wasted space).
For a NSString, there are different concrete subclasses for different situations. A string literal @"2"
will point to a statically allocated string literal object, a string created at runtime will probably have a different representation.
In general, I'd guess 4 bytes (your pointer) + 16 bytes (the NSString object) + number of characters * 2 (sizeof(unichar)) rounded up to multiples of 16.
To summarize, I estimate that NSNumbers
need about five times more memory than int
s. I further estimate that the same number represented as an NSString
takes about 10 times more more memory than an int
.
Also note that allocating objective-C objects is a lot slower than defining a local variable of type int. However, you should also remember that it will often not matter and that premature optimization is the root of all evil.