22

I was optimizing my app and wanted to know that how much is the size of the object, so that I can also show it in log.

suppose I have

NSDictionary *temp=(NSDictionary*)[Data objectAtIndex:i];

//data is defined in the .h file 

now how will I know that how much is the size of the object temp?

I tried using the variable view and in the temp section I found:

instance_size=(long int)30498656

Is the instance_size the exact size of my temp object?.

I also tried

sizeof(temp);

but it crashed on that point. Any help...?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46
  • 2
    answer to your question is in this thread. http://stackoverflow.com/questions/761969/checking-the-size-of-an-object-in-objective-c – AkaMu Nov 22 '11 at 08:00
  • You might want to check this [thread](http://stackoverflow.com/q/761969/936408) out. – Jesper Nov 22 '11 at 08:01
  • answer to your question is in this thread. http://stackoverflow.com/questions/761969/checking-the-size-of-an-object-in-objective-c – AkaMu Nov 22 '11 at 08:01
  • 1
    i got the size of temp object as 48..it is in Bytes, Kb or Mb??? – Ajeet Pratap Maurya Nov 22 '11 at 08:53

5 Answers5

31

The compiler knows only about the pointer, and that is why it will always return size of the pointer. To find the size of the allocated object try something like

NSLog(@"size of Object: %zd", malloc_size(myObject));
Legolas
  • 12,145
  • 12
  • 79
  • 132
  • 20
    you forgot to tell that you have add #import first. In iOS 5 when do it without adding the header it will give you warning. but thanks that worked.one more thing in the log it was showing the size as 48 for my temp object so it is in Mb or in KB or in Bytes..: :P – Ajeet Pratap Maurya Nov 22 '11 at 08:11
  • This will be bytes. Refer : http://www.9wy.net/onlinebook/CPrimerPlus5/ch03lev1sec4.html – DShah Aug 23 '12 at 09:36
  • 1
    C has a built-in operator called sizeof that gives sizes in bytes. (Some compilers require %lu instead of %u for printing sizeof quantities. That's because C leaves some latitude as to the actual unsigned integer type that sizeof uses to report its findings. C99 provides a %zd specifier for this type, and you should use it if your compiler supports it.) – DShah Aug 23 '12 at 09:37
  • 2
    You would need to add (__bridge const void *) for casting the custom Object type to (void *). __bridge is used to cast without changing the object's owner. – Gokul Dec 24 '14 at 05:42
11

First of all, I think it's clear from the above posts that the object size is given by malloc_size(myObject), as suggested by Legolas and also on the Mac OS reference manual:

The malloc_size(ptr) function returns the size of the memory block that backs the allocation pointed to by ptr. The memory block size is always at least as large as the allocation it backs, and may be larger.

But if you are interested in finding out the size of the dictionary, keep in mind the following point:

The dictionary stores key-value pairs and does not contain the object itself in the value part but just increases a retain count of the object that was to be "added" and keeps a reference of that object with itself. Now, the dictionary itself just contains the references to the various objects (with a key attached). So if by any chance you are looking for the object size of all the objects refered to by the dictionary, technically that would not be the size of the dictionary. The size of the dictionary would be the sum of the size of all the keys plus the size of all the value-references against the keys plus the size of the parent NSObject. If you are still interested in finding out the size of the refered objects as well, try iterating over the dictionary values array:

NSArray *myArray = [myDictionary allValues];
id obj = nil;
int totalSize = 0;
for(obj in myArray)
{
    totalSize += malloc_size(obj);
}
//totalSize now contains the total object size of the refered objects in the dictionary.

Reference

Alex Zavatone
  • 4,106
  • 36
  • 54
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
5

I would suggest using class_getInstanceSize and malloc_good_size to see what it'll round up to. This will not show the ivars and whatnot inside the returned size.

#import <malloc/malloc.h>
#import <objc/runtime.h>

NSLog(@"Object Size: %zd", malloc_good_size(class_getInstanceSize([yourObject class])));
EvilPenguin
  • 535
  • 6
  • 9
0

Variable temp is a memory address. We can not get the object memory size by programming. We can use the Allocations instrument to optimize app in xcode. Click Command+I in xcode4, you can find it.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
wangdg
  • 1
-1

In objective- C:

NSString *sampleString = @"iOS Programming";
 NSLog(@"size of object: %zd", malloc_size((__bridge const void *)(sampleString)));

You need to include:
#import<malloc/malloc.h>

In Swift:

var name: String = "Hello World!"

sizeofValue(name)

var someValue: Double = 10.5

sizeofValue(someValue)
KSR
  • 1,699
  • 15
  • 22
  • 3
    Docs for `sizeofValue` say `Does not include any dynamically-allocated or "remote" storage. In particular, sizeof(a), when a is a class instance, is the same regardless of how many stored properties a has.` So will not suffice for _any_ object. – Tim Windsor Brown Sep 16 '16 at 10:17