orak
orak

Reputation: 2419

How much memory is reserved when initializing NSObject?

When I use this statement in objective c

NSObject object = [[NSObject alloc] init];

How much memory is reserved for object?

Upvotes: 5

Views: 1046

Answers (3)

Wade Tregaskis
Wade Tregaskis

Reputation: 2018

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.

Upvotes: 0

Phillip Mills
Phillip Mills

Reputation: 31026

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

Upvotes: 0

J Shapiro
J Shapiro

Reputation: 3861

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"

Upvotes: 8

Related Questions