How much memory is reserved when initializing NSObject?

When I use this statement in object c

NSObject object = [[NSObject alloc] init]; 

How much memory is reserved for the object?

+4
source share
3 answers

You can check 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))); 

In this test appeared: "Size: 16 bytes"

+7
source

According to the tools, 16 bytes. (Although that NSObject *object = ... )

0
source

This depends on several factors, including whether you are running iOS or OS X, which version of the OS you created, and whether you are compiling a 32-bit or 64-bit version. This is either 8 bytes or 16 bytes. This is mainly 16 bytes, in later environments - there is a pointer to the data structure of the class, a reference counter and four bytes of different data specific to the data class.

Please note that the response you get from malloc_size () will always be 16, as it returns the size of the allocation block, which may be larger than the actual size requested by malloc. On OS X and iOS, the allocation size is always at least 16 bytes.

0
source

All Articles