Memory alignment of bytes occupied by different types of data
Bytes for different types of data
Memory alignment
In iOS 64-bit systems, 8-byte alignment is used (calculating the sum of attributes’ memory size), the minimum memory size is 16 bytes, and the actual allocated space is 16 bytes alignment (actual allocated space).
The attribute memory layout of an object follows these rules
- The first address of a structure variable is an integer multiple of its longest primitive type member;
- The offset of each member of the structure relative to the first address of the structure is an integer multiple of the size of the member. If not, the previous member is filled with bytes to meet the requirement.
- The total size of the structure is an integer multiple of the size of the structure’s largest primitive type member variable.
- Struct as members: If a struct internal member contains other struct internal members, the struct internal members are stored from an integer multiple of the maximum element size of their internal members
- The member variables in a structure are allocated in contiguous memory space.
- Different order of structure members will lead to different memory space. Objects optimized by the compiler do not have this problem
The output
How much memory an instance of NSObject takes up
#import <objc/runtime.h> #import <malloc/malloc.h> // ChildrenObject has age, name attributes, It's going to be a memory dummy ChildrenObject *obj = [[ChildrenObject alloc] init]; obj.age = 20; // class_getInstanceSize depends on <objc/ Runtime. h> and returns the size of memory required to create an instance object. Get the sum of the size of all the attributes of the object, and then align it with 8 bits, less than 8 bits complement 8 bits. NSLog(@"class_getInstanceSize = %zd", class_getInstanceSize([ChildrenObject class])); // malloc_size depends on <malloc/malloc.h>, returns the size of the memory allocated to the object, NSLog(@"malloc_size = %zd", malloc_size((__bridge const void *)obj)); // in 64-bit architecture, if you define an NSObject object, no matter how many member variables the object has, the sizeOf sizeOf will be 8 bytes, because sizeOf takes the memory allocated by the type, and the argument is a pointer type. So you end up with 8 NSLog(@"sizeof = %zd", sizeof(obj));Copy the code
Notice the difference between the results obtained by the three methods above
- Class_getInstance Gets the memory aligned size of the instance object
- Malloc_size gets the actual memory size allocated by the system
- SizeOf the sizeOf a type in bytes, always 8 if an object is passed; If it’s a structure instance that gets
extension
-
Why is the actual memory space allocated by the iOS system a multiple of 16? What are the corresponding policies
Libmalloc analyzes multiples of 16 for efficient access and utilization, with tiny, small, and large policies
-
Union, structure, similarities and differences of objects
- The difference between a structure and a class
- Struct value type. Class is a reference type. Structure variable assignment is direct assignment, value transfer; Object assignment is pointer passing
- Structs do not inherit properties; classes do
- Structs have default constructors; Class needs to build its own constructor,
- Structs can only encapsulate properties, and classes can encapsulate properties and methods
- Structure variables are allocated on the stack, OC objects are allocated on the heap; Stack space is small, fast access; The heap space is large and the access speed is slow
- The difference between a structure and a union
- Structure is
- What is a union
- The difference between a structure and a class
-
Structure has object members (NSString, eg), object has structure properties, what’s the problem
-
If you add methods to a class, does the memory size of the class instance object change? Why? Why
-
When an object is created, memory is not allocated to its methods, only to its properties and member variables
-
A class may create multiple instances, each instance of the method is the same, there is no difference, can be completely removed, all objects share the memory of the storage method, in fact, are stored in the class instance, a class only one instance, created by the system.
-
The benefits of this design are space saving, faster initialization, etc
-
data
Sohu NSObject Memory layout