Total Size of NSMutableArray object

72,358

Solution 1

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.

Solution 2

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

Note that class_getInstanceSize is declared in /usr/include/objc/runtime.h

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

Solution 3

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.

Solution 4

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.

Solution 5

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 exactly how 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.

Share:
72,358
Admin
Author by

Admin

Updated on March 05, 2020

Comments

  • Admin
    Admin about 4 years

    I've got an NSMutableArray that holds a bunch of objects, what I'm trying to figure out is how much memory is the array using. 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 NSMutableArray object it self).

    Example code:

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

    now I want to know the size :)

  • Quinn Taylor
    Quinn Taylor over 14 years
    The array actually only holds pointers to the object, so this would calculate the aggregate size of objects stored in the array, but doesn't count object that appear more than once, and won't tell you the actual memory consumed by temp itself.
  • Quinn Taylor
    Quinn Taylor over 14 years
    Agreed. Using -count will tell you how many objects are in the array, and you can multiply by the size of a pointer to get a rough estimate, but the array can use much more space (particularly a mutable array) so this can turn out to be a lowball guess anyway. Depending on what your goal is, it may or may not be more helpful than summing the sizes of the instances it holds. In general, I agree with @cobbal that multiple ownership complicates things and makes it less important to know exact sizes. For large arrays or large objects, yes, but as always, measure performance first. :-)
  • Quinn Taylor
    Quinn Taylor over 14 years
    Has pretty much the same shortcomings as the answer by NSResponder.
  • Ed Marty
    Ed Marty over 14 years
    I guess the question is how accurate do you need the result to be, and does it need to work in all cases for any possible object. If you know the sorts of objects you are going to be adding to the array, it's much easier to determine the memory usage.
  • Ed Marty
    Ed Marty over 14 years
    Additionally, if you are adding arrays to arrays, you can sum up those sizes with recursive calls to the summing function for any object that implements NSFastEnumerator