The definition of NSObject

NSObject is the base object type of OC, which is wrapped in C/C++ constructs for object-oriented encapsulation. In the runtime source code, NSObject isa wrapper around Class isa.

Class
objc_class

isa

Before arm64, ISA was simply a pointer to the memory address of a Class or meta-class object. However, after arm64, isa was optimized to become a Union shared structure, similar to struct structure, but multiple members of a union share the same memory, which is the amount of memory required by the longest type of member.

ISA_BITFIELD

  • Nonpointer: 0, a pointer that stores the memory address of a Class or meta-class object. 1, represents optimized, using bitfields to store more information
  • Has_assoc: is the associated object set? If not, it will be released faster
  • Has_cxx_dtor: does C++ destructor (.cxx_destruct) exist? If not, it will be released faster
  • Shiftcls: Stores the memory address information of Class and meta-class objects
  • 5. Magic: Used to distinguish whether an object has not completed initialization during debugging
  • 6, Weakly_referenced: Whether there is a weak reference to point to, if not, the release will be faster
  • Deallocating: Whether the object is being released
  • Extra_rc: Stores values in the reference counter
  • Has_sidetable_rc: whether the reference counter is too large to be stored in ISA; If it is 1, the reference count is stored in an attribute of a class called SideTable

The number after each field indicates the number of bits the field occupies. The total length is still 64 bits (8 bytes), which is still the size of a pointer. With the union structure, the ISA pointer holds more information and saves memory.

Memory size of the NSOBject object

To check the memory size of an object, we use two apis

// Runtime. h Actual memory size NSLog(@"%zd", class_getInstanceSize([NSObject class])); // output 8 //malloc.h Actual allocated memory size NSLog(@"%zd", malloc_size((__bridge void *)obj)); 16 / / outputCopy the code

class_getInstanceSize

To allocate memory when creating objects, we use the alloc method. In the source code, methods are called in this order (allocWithZone is also similar, and ends up at the same location).

Finally, it falls to the _class_createInstanceFromZone method

malloc_zone_calloc
calloc
size_t size = cls->instanceSize(extraBytes);

You can see that when allocating memory, the allocated size is alignedInstanceSize + the extra size passed in, and automatically expands to 16 bytes when size is less than 16 bytes.

So we have two pieces of information

  • class_getInstanceSizeThe object () method returns the actual size of the memory that the object needs to use, with an alignment in the structure (the maximum data type is isa pointer) and a multiple of 8 bytes (pointer size).
  • malloc_sizeMethod returns the size of memory allocated by the system to the object, in multiples of 16 bytes, depending on alignment strategies.

OC Object classification

OC objects can be divided into three main types

  • Instance An instance object, consisting of a classallocOut of the object each time it is calledallocCreates a new instance object.
  • Class class objects. Each class has one and only one class object in memory.
  • Meta-class metaclass objects, each class has one and only one metaclass in memory.

Instance objects

  • Isa pointer
  • Other member variables (specific values)

Object like storage

  • Isa pointer
  • SuperClass pointer
  • Member variables (descriptive information about member variables, such as variable types, not specific values)
  • Class object methods (-Instance method)
  • Class protocol information (Protocol)
  • Class property information (@Property)

Metaclass object storage

  • Isa pointer
  • Superclass pointer
  • Class methods (+Methods)

Summary of three types of diagrams