aNSObjectHow much memory does an object take up?

Due to the memory allocation mechanism, an NSObject object is allocated 16 bytes of memory.

But in fact, in 64-bit, only 8 bytes are used; In 32 bits, only 4 bytes were used

The size of a member variable of an NSObject instance object is actually 8 bytes

#import <Objc/Runtime>
Class_getInstanceSize([NSObject Class])
Copy the code

Nature is

size_t class_getInstanceSize(Class cls)
{
    if(! cls)return 0;
    return cls->alignedInstanceSize();
}
Copy the code

Gets the size of memory pointed to by the Obj -c pointer, which is actually 16 bytes

#import <malloc/malloc.h>
malloc_size((__bridge const void *)obj); 
Copy the code

Object memory is aligned when allocating memory, so in iOS, memory is allocated in multiples of 16 bytes.

Can through the following url: openSource.apple.com/tarballs to view the source code.

Hot article recommended

Interview Questions for iOS