5

When I use this statement in objective c

NSObject object = [[NSObject alloc] init];

How much memory is reserved for object?

3 Answers 3

8

You can test the size of objects with the following code:

#import <malloc/malloc.h>
//...
NSObject *obj = [[NSObject alloc] init];
NSLog(@"Size: %zd bytes", malloc_size((__bridge const void *)(obj)));

This test produced: "Size: 16 bytes"

1
  • and the answer this gives is 16 bytes.
    – emrys57
    Dec 2, 2012 at 14:40
0

According to Instruments, 16 bytes. (Though, that's NSObject *object = ...)

0

It depends on several factors, including whether you're on iOS or OS X, what OS version you've built against, and whether you're compiling 32-bit or 64-bit. It's either 8 bytes or 16 bytes. Basically it's 16 bytes, in more recent environments - there's a pointer to the class data structure, a reference count and four bytes of miscellaneous, class-specific data.

Note that the answer you get from malloc_size() will always be 16, because it returns the size of the allocation block, which may be larger than the actual size requested of malloc. In OS X and iOS the allocation size is always at least 16 bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.