Objective-C中类的内存大小

Objective-C中类的内存大小

问题描述:

我想知道内存的大小,分配一些Objective-C对象.

I'm interested in knowing the size of memory, allocating some Objective-C objects.

例如:

[NSString stringWithString:@"2"]是否大于[NSNumber numberWithInt:2]?

[NSNumber numberWithInt:2]int num=2大多少?

Apple是否提供有关此问题的一些文档?我认为这些信息对于优化内存非常重要.

Is there some documentation from Apple about this question? I think this information is very important for memory optimisation.

确切的文档不可用. NSString和(IIRC)NSNumber被实现为类集群,即,当您请求一个新对象时,您可能实际上得到了一些未记录的子类的对象.

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.

这还意味着当您的程序在不同的OS版本上运行时,情况可能会发生变化,而不会发出警告,因此请不要依赖确切的数字.

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.

现在,让我们尝试一个大概的估计.当前所有Apple平台上的整数均为4字节. 指针在iOS上为4个字节.

Now, let's try a rough estimate. Integers are 4 bytes on all current Apple platforms. Pointers are 4 bytes on iOS.

对象在堆上分配;在最低级别上,堆分配由malloc完成. 我将假定iOS的malloc实现是从Mac OS上使用的实现派生的-请在此处查看一些详细信息:

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

最重要的一点是,小对象的分配量为16个字节,即小对象将使用16字节的倍数.

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.

每个Objective-C对象都包含一个指向其类的指针.

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

因此,对于一个包含int的NSNumber,我估计您的指针需要4个字节,外加对象的16个字节(由4个字节的类指针和-我猜-一个4个字节的int加上8个字节组成浪费的空间).

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).

对于NSString,针对不同情况有不同的具体子类.字符串文字@"2"将指向静态分配的字符串文字对象,在运行时创建的字符串可能具有不同的表示形式. 通常,我猜4字节(您的指针)+ 16字节(NSString对象)+字符数* 2(sizeof(unichar))四舍五入为16的倍数.

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.

总而言之,我估计 NSNumbers需要的内存大约是int的五倍.我进一步估计,表示为NSString的相同数字所花费的内存大约是int的10倍.

To summarize, I estimate that NSNumbers need about five times more memory than ints. I further estimate that the same number represented as an NSString takes about 10 times more more memory than an int.

还要注意,分配Objective-C对象比定义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.