A lot of people know that objective-C is primarily implemented in C and C++, so objective-C objects must be implemented based on C and C++ data structures. Which begs the question, what data structure implements it? You can imagine, object or a class can have various types of instances including nsstrings, Float, int, NSArray, you want to store different types of data structures, only the structure. Open NSObject and see the source code:Clicking on Class makes it clear that it is a structure:Of course you can turn OC into C++ code to find out and write a macos project

int main(int argc, Const char * argv[]) {@autoreleasepool {// NSObject *objc = [NSObject new]; }}Copy the code

Use terminal command to convert main.m file to C++ file :xcrun -sdk iphoneos clang -arch arm64-rewrite-objc main.m -o main.cpp

Xc: abbreviation xcrun: a tool of Xcode. – SDK iphoneOS: specifies that the SDK needs to run on iOS. Clang :Xcode is a built-in LLVM compiler front-end, which is also a compiler. – Arch XXX (ARM64, I386, ARMV7…) Rewrite-objc xx.m is an instruction to rewrite objc code (i.e., to rewrite xx.m files). -o newfilename. CPP means to output a new.cpp file

There are tens of thousands of lines of code in the.cpp file, and we don’t have to go there. We can just search NSObject_IMPL{and we’ll see that NSObject is the equivalent of:So the system’s NSObject class has only one member variable, the ISA pointer

So how much memory does an NSObject take up? Let’s look at the function and see how much it takes up, and then we’ll talk about how it works

NSLog(@"NSObject instance size %zd",class_getInstanceSize([NSObject class])); NSLog(@"malloc --%zd ",malloc_size((__bridge const void *)(objc)));Copy the code

I’m sure some of you don’t understand where those two functions come from and why they’re used, so explain

The <objc/ Runtime. h> file provides the class_getInstanceSize(Class _Nullable CLS) method, which returns the memory size of an instance of an OC object.

The <malloc/malloc.h> file that provides size_t malloc_size(const void * PTR) returns the size of memory allocated by the system for this object.

So far we know that an NSObject requires 8 bytes of memory. The system actually allocates 16 bytes of memory. Why is that? Before you worry, let’s look at another example

Define a Person class:

@interface Person : NSObject
{
    @public
    int _height;
    int _age;
    NSString *_name;
}
@end
Copy the code

We’ve created a Person class that has two int member variables in it, an NSString member variable, and as you know, an int is 4 bytes and an NSString is 8 bytes, so now how much memory does a Person instance need? Let’s convert it to a C++. CPP file:NSOject_IMP contains only one isa pointer, which is 8 bytes long, and can be used to calculate the size of the memory.8+4+4+8 requires 24 bytes ESize, let’s see if it’s 24 bytes and that takes 24 bytes, how many bytes does the system allocate? I use malloc_size and I get 32 bytes allocated to it, okay? How exactly is it distributed? Let’s go ahead and add a member variable to the Person classCalculate the memory 8+4+4+8+4 = 28, but you print out exactly 32 and 32 using class_getInstanceSize and malloc_size respectively, so the instance needs 32 bytes of memory, and the system allocates 32 bytes of memory for it

The amount of memory an OC takes up depends on how many of the object’s members are easy to understand, but how can it be that the computation required is not the same as the printed instance required and allocated? There is a memory alignment problem here. In iOS, the system allocates memory in multiples of 16 bytes, up to 256 bytes, which is the best and fastest way for the operating system to access the CPU. How much memory an object needs depends on the size of the maximum member variable, which must be a multiple of the maximum member variable