ios NSMutableArray 对象的总大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1515538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:29:27  来源:igfitidea点击:

Total Size of NSMutableArray object

iosobjective-ccocoa-touchnsmutablearray

提问by Sophie Alpert

I've got an NSMutableArraythat holds a bunch of objects, what I'm trying to figure out is how much memory is the arrayusing. After looking at a couple of places I know about the size of call, and when I make it I get 32 bits (which is the size of the NSMutableArrayobject it self).

我有一个NSMutableArray包含一堆对象的对象,我想弄清楚正在使用多少内存array。在查看了几个地方之后,我知道调用的大小,当我调用它时,我得到 32 位(这是NSMutableArray对象本身的大小)。

Example code:

示例代码:

NSMutableArray *temp = [[NSMutableArray alloc]init];
[temp addObject:objectxyz];
[temp addObject:objectabc];
[temp addObject:object123];

now I want to know the size :)

现在我想知道尺寸:)

回答by Sophie Alpert

To get the number of objects in the array, use

要获取数组中的对象数,请使用

[temp count]

If you want the total memory usage of the array, you'll have to loop through and add up how much memory each object uses, but I don't think that a generic object will give you its size. In general, you shouldn't really have to worry about memory usage, though.

如果您想要数组的总内存使用量,则必须循环并累加每个对象使用的内存量,但我认为通用对象不会给您它的大小。不过,一般来说,您真的不必担心内存使用情况。

回答by Ed Marty

size_t size = class_getInstanceSize([temp Class]);
for (id obj in temp) {
    size += class_getInstanceSize([obj Class]);
}

Note that class_getInstanceSizeis declared in /usr/include/objc/runtime.h

注意class_getInstanceSize在 /usr/include/objc/runtime.h 中声明

Also note that this will only count the memory size of the ivars declared in each class.

另请注意,这只会计算每个类中声明的 ivars 的内存大小。

回答by cobbal

There is no direct way to do this since all objects are just stored by reference. There is no concrete notion of "size" in cocoa, especially since objects can have multiple owners which might lead to double counting or other problems.

没有直接的方法可以做到这一点,因为所有对象都只是通过引用存储。可可中没有具体的“大小”概念,特别是因为对象可以有多个所有者,这可能会导致重复计算或其他问题。

回答by hotpaw2

If the NSArray, and all the objects it contains, and all their sub-objects (recursively etc.) respond to NSCoder, you might be able to serialize the array into a temporary NSData memory chunk, and then get the memory size of that one flat temporary object.

如果 NSArray 及其包含的所有对象及其所有子对象(递归等)响应 NSCoder,则您可以将数组序列化为临时 NSData 内存块,然后获取该数组的内存大小平面临时对象。

回答by NSResponder

Well, you could do something like:

好吧,你可以这样做:

size_t total;
id obj;
for (obj in temp)
  {
  total += class_getInstanceSize([obj class]);
  }

but that doesn't tell you exactlyhow much storage the array is actually using, since it can grow dynamically and might have more memory at any given time than it needs for just the objects it's pointing to, and of course you'd have to deal with any collections recursively.

但是,这并不告诉你究竟多少存储阵列实际使用,因为它可以动态地增长,可能比它需要的只是对象它所指向任何特定时间拥有更大的内存,当然你必须递归处理任何集合。

If you're trying to get an idea of how much memory you're using, I suggest digging into the tutorials for Instruments, and getting your head around the memory usage probes it provids.

如果您想了解您使用了多少内存,我建议您深入研究 Instruments 的教程,并了解它提供的内存使用情况探针。