所有对象占用的内存量都相似吗? Objective-c

发布于 2024-10-04 21:07:56 字数 114 浏览 8 评论 0原文

如果我使我的对象成为 UIViewController 的子类,它是否比 NSObject 的子类使用更多的内存?一个关于复杂类的子类化与简单类的子类化所使用的开销的大概数字会很棒。编辑:或者我自己找出差异的方法。

If I make my object a subclass of UIViewController, does it use substantially more memory than if it is a subclass of NSObject? A ballpark figure for how much more overhead is used by subclassing a complex class vs a simple one would be great. edit: or a way to figure out the difference myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谜兔 2024-10-11 21:07:56

您可以想象,objective-c 对象只是一个如下所示的 C 结构:

typedef struct {
    Class isa;
} NSObject;

该结构的实例在 32 位系统上占用 4 个字节。由于它由单个指针组成 - Class 类似于 id。

NSObject 的子类 MySubclass 具有一个“char”实例变量,看起来像这样:

typedef struct {
    Class isa;
    char singleInstanceVariable.
 } MySubclass;

子类在开头简单地具有其超类的所有实例变量,在末尾加上它自己的实例变量。您可以通过在控制台中键入“p *object”在调试器中看到这一点。

在 32 位系统上,MySubclass 的大小为 5 个字节。一个指针,加一个字符。因此,对象的大小是其所有实例变量的大小之和。要知道的一件重要的事情是对象的大小仅与其实例变量相关。它不受其拥有的方法数量的影响。随着实例化更多实例,这些方法不会消耗任何额外的内存。方法具有固定的初始成本。

另一件需要考虑的事情是,对象通常具有指向其他对象的指针作为实例变量。例如,假设每个 UIView 都有一个 NSMutableArray 来引用其子视图。该数组为空时可能为 12 个字节。因此,空 UIView 将是 UIView 中所有变量的大小,其中包括指向数组的指针的 4 个字节,此外您还可能考虑实际数组实例使用的 12 个附加字节。这只是会计,数组和视图是两个不同的对象,但如果没有数组,视图就无法真正使用。

最后,大多数分配都会向上舍入到某个量子,以便使 malloc 实现更快并满足机器体系结构的某些约束,以便指针正确对齐。此外,对象的实例变量之间可能有空填充,类似于结构填充

You can imagine that an objective-c object is just a C structure that looks like this:

typedef struct {
    Class isa;
} NSObject;

An instance of that structure would take 4 bytes on a 32-bit system. Since it's composed of a single pointer - Class is similar to id.

A subclass of NSObject, MySubclass with one 'char' instance variable would look like this:

typedef struct {
    Class isa;
    char singleInstanceVariable.
 } MySubclass;

A subclass simply has all of the instance variables of its super class at the beginning, plus its own at the end. You can see this in the debugger by typing 'p *object' in the console.

MySubclass's size would be 5 bytes on a 32-bit system. One pointer, plus one char. So, an object's size is the size of all of it's instance variables added together. One important thing to know is that an object's size is only related to its instance variables. It isn't impacted by the number of methods it has. Those methods don't cost any extra memory as more instances are instantiated. Methods have a fixed initial cost.

Another thing to consider is that objects usually have pointers to other objects as instance variables. For example, let's say every UIView has an NSMutableArray to reference its subviews. That array might be 12 bytes when empty. So an empty UIView would be the size of all of the variables in a UIView, which would include 4 bytes for the pointer to an array, plus you might also account for the 12 additional bytes used by the actual array instance. That's all just accounting though, the array and the view are two distinct objects, but the view isn't really usable without the array.

Lastly, most allocations are rounded up to some quantum in order to make the malloc implementation faster and to satisfy some constraints of the architecture of the machine so that pointers are properly aligned. Also an object's instance variables might have empty padding in between them similar to structure padding

少跟Wǒ拽 2024-10-11 21:07:56

这取决于超类实例变量的数量和性质。 NSObject 有一个 ivar,即 isa 指针。 UIViewController 有大约 30 个 ivars,其中大多数是指针(您可以在 UIViewController.h 中查找列表)。

因此,UIViewController 的任何子类都将占用存储所有这些 ivars 及其超类的所有 ivars 所需的内存(UIResponder(无 ivars)和 NSObject(一个 ivar))。

当然,此计算不考虑这些实例变量在初始化时引用的对象所使用的实际内存。例如,完全初始化的视图控制器可能会保留占用大量内存的视图对象。

That depends on the number and nature of the superclass's instance variables. NSObject has one ivar, the isa pointer. UIViewController has about 30 ivars, most of them pointers (you can look the list up in UIViewController.h).

So any subclass of UIViewController will take up as much memory as is needed to store all those ivars and all ivars of its superclasses (UIResponder (no ivars) and NSObject (one ivar)).

This calculation does not take into account the actual memory that is used by the objects these instance variables reference when initialized, of course. For example, a fully initialized view controller may hold on to a view object that takes up a considerable amount of memory.

不羁少年 2024-10-11 21:07:56

尝试class_getInstanceSize([MyClass class]);。粗略地说,实例的内存使用量将是该值四舍五入到十六字节的倍数。当然,这不包括任何关联对象的开销(请参阅 objc_setAssociatedObject )或类所做的分配。

Try class_getInstanceSize([MyClass class]);. Roughly speaking, the memory usage of an instance will be this value rounded up to a multiple of sixteen bytes. Of course, this doesn’t include overhead of any associated objects (see objc_setAssociatedObject) or allocations the class makes.

浅忆流年 2024-10-11 21:07:56

简而言之,是的,但可能还不足以让您需要担心,除非您计划实例化数以万计的它们。

该对象将为它的每个 ivar 及其方法分配内存。所需的内存量取决于 C 类型...它们都根据所存储的数据类型而变化。

In short, yes, but probably not enough that you need to worry about it unless you're planning on instantiating tens thousands of them.

The object will allocate memory for each of its ivars and its methods. The amount of memory needed depends on the C types... they all vary according to the datatype what's being stored.

后知后觉 2024-10-11 21:07:56

使用的内存量取决于对象的实例化方式、在屏幕上的呈现方式以及交互方式。例如,NSObject 的子类不会与用户的触摸进行任何交互。

您始终可以将您的应用程序与仪器分配性能工具一起附加以比较差异。

The amount of memory used depends on how the object is instantiated, presented on screen, and interacted with. For example, a subclass of NSObject will not have any interaction with the user's touches.

You can always attach you application with the Instruments Allocations performance tool to compare the difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文