Object-C memory management-special case of Object reference count

Source: Internet
Author: User

We can see the memory management section in OC, where the reference counting section, the EBOOK sample on part 10.5 has been running correctly on part 10.9, for example, the following code:

NSString * str1 = @ "string 1 ";

NSString * str2 = @ "string 2 ";

NSMutableString * mstr3 = [NSMutableString stringWithString: @ "string 3"];

NSNumber * int1 = [NSNumber numberWithInteger: 1800000];

NSNumber * long1 = [NSNumber numberWithLong: 1800000];

NSNumber * double1 = [NSNumber numberWithDouble: 300.0];

    

NSLog (@ "retain count: str1 = % lx, str2 = % lx, str3 = % lx, int1 = % lx, long1 = % lx, double1 = % lx ",

[Str1 retainCount],

[Str2 retainCount],

[Mstr3 retainCount],

[Int1 retainCount],

[Long1 retainCount],

[Double1 retainCount]);

The running result is:

014-11-07 17:05:22. 128 Demo17 [9620: 303] retain count: str1 = ffffffffffffff, str2 = ffffffffffffff, str3 = 1, int1 = 7 fffffffffffffff, long1 = 7 then, double1 = 1

Program ended with exit code: 0

In the EBOOK, it is correct that constant string objects do not participate in counting, but the actual running result shows that the NSNumber object of the integer type does not seem to participate in counting, and the NSNumber object of the floating point is involved in counting, even if the integer is large, it is not counted. Java used to cache small-number encapsulated objects. All small-number encapsulated objects are not counted. But it does not look like this in OC, because numbers are large enough to cache so many integer objects, right?

What is OC? Continue with the study.

The conclusion is as follows:

1. Constant strings are not counted.

2. Constant integer NSNumber objects are not counted.

3. Other objects participate in the count.

 

After finishing the work, record the problem ~

Object-C memory management-special case of Object reference count

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.