Memory management solution technology

  • Tagged Pointer(marker pointer), used to handle small objects NSNumber, NSDate, NSString
  • Nonpointer_isa:, non-pointer type. This is simply 64-bit binary data, not a pointer to storage, and the other bits are used to store some other information about the OC object
  • sideTables: hash table, includingReference counter tableandA weak reference table

The memory management section covers some of the above techniques, but today’s article focuses on Tagged Pointer

WWDC video

It is highly recommended that you first take a look at the introduction of Tagged Pointer on apple’s official video. The video address is WWDC video. It is suggested to start watching from 15:28

  • What is tagged Pointer?

In a 64 – bit address, we didn’t really the use of all these, we are only in a true object pointer is used in the middle of some, as a result of the alignment requirements status is 0, the object must always, the address of a pointer size ratio, because the address space is limited, so high is always a 0, we won’t actually use all of the 64, Since the high and low levels are always bit 0, we can set these positions to bit 1 so that they are not a true pointer. We can give these bits some other meaning. We call such an address tagged Pointer

  • Tagged Pointer is used to store small objects, such as NSNumber and NSDate
  • The Tagged Pointer value is no longer an address, but a real value. So, it’s not really an object anymore, it’s just a normal variable in an object’s skin. Therefore, its memory is not stored in the heap and does not require malloc and free
  • Three times more efficient at memory reads and 106 times faster at creation.

Tagged Pointer Indicates data confusion

When dyld loading class _read_images – > initializeTaggedPointerObfuscator

We can see that every time the program starts it generates a random numberobjc_debug_taggedpointer_obfuscator, and then encode and decode by this number

Coding _objc_encodeTaggedPointer

The code actually does xOR, does something different on the real machine. We have used x86_64 as an example.

Decoding _objc_decodeTaggedPointer

Encoding and decoding examples, ifobjc_debug_taggedpointer_obfuscator = 1010 1010The originalptr= 0101 1110.

Code: 0101 111 ^ 1010 1010 = 0000 1010

Decoder: 1010 1010 ^ 1010 1010 = 0101 1110

Setting environment variables

We can control obfuscation by setting environment variable Settings, as shown below

Xcode set

OBJC_DISABLE_TAG_OBFUSCATION = YESClose the confusion

Tagged Pointer data structure

inobjcFind the code belowPass whethertagged poniterMethod, the _OBJC_TAG_MASK field. Under the MAC OS _OBJC_TAG_MASK = 1In iOS, the lowest order is 1define _OBJC_TAG_MASK (1UL<<63)After debugging, it is found that tagged Pointer structure will be different under different versions of real computers. The following result is that I debug LLDB under iOS 14.5 and print the address in binary

LLDB debugging, printing addresses in binary (p6-juejin.byteimg.com/tos-cn-i-

  • 0b indicates binary. The highest bit 1 indicates tagged pointer.0-1Presentation type:010 = 2saidNSString.011 = 3Said,NSNumberYou can verify this in the objC source code

  • NSString:3-6Bit, representing the length of the string. For example, 0010 = 2 is a string of 2 characters
  • NSNumber:3-6Bit indicates that int, float, and double are stored

Data Store Verification

Real 14.5, 7-62 bits used to store real data, let’s verify.

NSString: 7-62:0 omitted: 0110 0010 0110 0001. Let’s look at the ASCII table

That’s a and B of strings

NSNumber :7-62: the preceding 0 is omitted, so 0110 is exactly 6 in decimal

Summary of data structure

Use a diagram to summarize the storage structure of real machine 14.5.

Of course, this diagram is just my storage under 14.5, different versions may have different locations. If you’re interested, you can look into it.

NSString memory management

So NSString must be Tagged Pointer. So let’s do a test.

  • Initialize string with string, string with string, and string with format, initWithFormat, respectively
  • The length of the initialized string is different,The < 9.And greater than 9

The code is as follows:

Print result:

As you can see from the print above, there are three types of NSString.

  • __NSCFConstantString: is a string constantCompile-time constant, retainCount is a large value,Does not cause a reference count change, stored in the string constants area. It’s initialized directly with @””, initWithString, StringWithString

-nSTAGgedPointerString: is the tagged pointer type mentioned above. This is an Apple optimization, but the string length should not be too long. NSString type, Tagged Pointer type you can see here’s a table.

  • __NSCFStringThis is the object type that allocates memory, increases the reference count, and stores it on the heap

conclusion

  1. Tagged Pointer is actually an optimization of apple’s heap memory that can be used for (NSString, NSNumber, NSDate). Its data is stored directly in Pointers

  2. Since Tagged Pointer is not stored on the heap, it is easier and more efficient to access

  3. Reduce unnecessary waste, save memory space.