1, a few large areas of memory1>: Local variables (base data type, pointer variable). 2> Heap: Dynamically allocated storage space (objects created) during program run. 3> BSS segment: no initialized global variables and static variables. 4> Data area: Global variables and static variables that have been initialized. (String constants)5> Code Snippet: The contents of the code after the program compiles. 2. Reference Counter1> Reference counters: Each object that inherits from NSObject has a reference counter to indicate that the current object has several owners. 2> The role of a reference counter: To determine whether an object should be recycled. 3> Reference counter operation (cannot be used under ARC)* Retain reference counter +1
* Release Reference counter-1
* Retaincount Gets the value of the reference counter ( Click to enter NSObject.h file, there is one-(Nsuinteger) method of Retaincount objc_arc_unavailable )Note: Any OC object, as long as one is created, its reference count defaults to 1, and when the reference count of an object is 0, the object is destroyed. 3, about the Dealloc methodThe 1> object is automatically called when it is disposedThe Dealloc method of the object is often rewritten in 2> development, which is used to observe when an object is released or to do something appropriate before the object is released, and the Dealloc method can be regarded as the "last words" of the object. 3> under MRC , rewrite dealloc to be aware of the need to call [Super Dealloc] at the end of it. 4, Zombie object, wild pointer, null pointer. 1> Zombie object: Refers to objects that have been destroyed (objects that can no longer be used)2> Pointer: A pointer to a zombie object (memory not available), sending a message to the wild pointer will report a "exc_bad_access" error (common error). 3> NULL pointer: There is no pointer to storage space (nil), no response to sending a message to a null pointer, just imagine how the method can react with nil. The way to resolve a wild pointer error is to change the pointer of the object to a null pointer.
// Turn on Zombie object detection first *obj = [[NSObject alloc] init]; [obj release]; // Wild Pointer Error [obj log:@ " test "];
Output Station Printing
Memory Management [2688:205046] * * * *-[nsobject log:]: Message sent to deallocated instance 0X100113C80 (message sent to an instance object that has been disposed)
5. Common Operations on ARC (automatic memory Management) and MRC (Manual memory Management) in development1> the project from the ARC environment to the MRC environment (which is now basically an arc environment, remain the default)enter "Auto" in the search boxChange "yes" to "NO", that is, change from arc to MRC, and vice versa to "yes" from MRC to ArcThe overall environment of the 2> project is arc, allowing a class in the project to support MRCdevelopment, we may sometimes use some of the previous classes, and some older class library is the MRC environment, then you can choose to increase the compiler switch "-fno-objc-arc" (here in the case of the Gdataxmlnode Class)In addition to the "Libxml.dylib" Dynamic library error, the first time to import Gdataxmlnode project, there will be about 20 errors, as the operation can3> detect Wild pointer (Zombie object) Error6, Nil, Nil, NULL1> Nil is an object value (empty object) that equals nil. Prevents calling Zombie objects to error2> Nil is a class object (the class object is empty)3> Null is a general-purpose pointer (generic pointer)4> [NSNull null] is an object used in situations where nil is not allowed. 7. @property Parametersmemory management of 1> control set methodretain:release old value, retain new value (for OC object)Assign: Direct assignment, no memory management (default, for non-OC object types)copy:release old value, copy new value (typically used for nsstring, guarantees string security)2> control requires no set method to be generatedReadWrite: Generate both the Set method and the Get method (default)ReadOnly: Only the Get method is generated 3> Multithreading ManagementAtomic: Low performance (default)nonatomic: High Performance4> controls the name of the set method and the Get methodsetter: Set the name of the set method, there must be a colon:Getter: Set the name of the Get method8, NSString in memory management problems
//recently seen on the online video on an interesting question, as the video says//string has a constant pool//If you need a string that already exists in the constant pool, no memory space is allocated//when using strings: strings obtained in the following 1, 3, and 5 are in the constant area (well, these three NSString objects are exactly in the character constant area, as the video says, because the reference count of the print is a huge number, so judging its memory does not belong to the user tube)//Note: The video says that str2 and STR4 should be allocated in the heap area, that is, the memory belongs to the user tube, the reference count should be 1//But in fact, look at the print below str2 and STR4? Suddenly messy, why, say yes in the heap area? NSString*STR1 =@"ABC"; NSString*STR2 = [NSString stringWithFormat:@"AAA"]; NSString*STR3 = [NSString stringwithstring:@"ABC"]; NSString*STR4 = [[NSString alloc] Initwithformat:@"AAA"]; NSString*STR5 = [[NSString alloc] initwithstring:@"ABC"]; NSString*STR6 = [[NSString alloc] init];//in the stack area? Why is its reference count so large? NSLog (@"str1 =%@,%p,%lu", Str1,str1,str1.retaincount); NSLog (@"str2 =%@,%p,%lu", Str2,str2,str2.retaincount); NSLog (@"STR3 =%@,%p,%lu", Str3,str3,str3.retaincount); NSLog (@"STR4 =%@,%p,%lu", Str4,str4,str4.retaincount); NSLog (@"STR5 =%@,%p,%lu", Str5,str5,str4.retaincount); NSLog (@"STR6 =%@,%p,%lu", Str6,str6,str5.retaincount);
//Print
2016-01-21 21:26:36.979 nsstring Memory management issues [3269:255674] str1 = ABC, 0x100004230, 18446744073709551615
2016-01-21 21:26:36.980 nsstring Memory management issues [3269:255674] str2 = AAA, 0x61616135, 18446744073709551615
2016-01-21 21:26:36.981 nsstring Memory management issues [3269:255674] str3 = ABC, 0x100004230, 18446744073709551615
2016-01-21 21:26:36.981 nsstring Memory management issues [3269:255674] Str4 = AAA, 0x61616135, 18446744073709551615
2016-01-21 21:26:36.981 nsstring Memory management issues [3269:255674] STR5 = ABC, 0x100004230, 18446744073709551615
2016-01-21 21:26:36.981 nsstring Memory management issues [3269:255674] str6 =, 0x7fff7c51bd00, 18446744073709551615
//Then say it's not the same under iOS, I'll try again, it's a different memory address, but what's the reference count?
2016-01-21 21:30:42.632 nsstring Memory Management [3289:257683] str1 = ABC, 0x106f4c050, 18446744073709551615
2016-01-21 21:30:42.633 nsstring Memory Management [3289:257683] str2 = AAA, 0xa000000006161613, 18446744073709551615
2016-01-21 21:30:42.633 nsstring Memory Management [3289:257683] str3 = ABC, 0x106f4c050, 18446744073709551615
2016-01-21 21:30:42.633 nsstring Memory Management [3289:257683] Str4 = AAA, 0xa000000006161613, 18446744073709551615
2016-01-21 21:30:42.633 nsstring Memory Management [3289:257683] STR5 = ABC, 0x106f4c050, 18446744073709551615
2016-01-21 21:30:42.633 nsstring Memory Management [3289:257683] str6 =, 0x107279380, 18446744073709551615
//Finally, I don't give up, try again
Nsarray *array1 = [[Nsarray alloc] init];
NSLog (@ "array1 =%lu", array1.retaincount);
Nsmutablearray *array2 = [[Nsmutablearray alloc] init];
NSLog (@ "array2 =%lu", array2.retaincount);
Print
2016-01-21 21:48:01.828 nsstring Memory management issues [3447:267148] array1 = 2 //Mother Egg Why is 2
2016-01-21 21:48:01.828 nsstring Memory management issues [3447:267148] array2 = 1
At last
See finally, there is such a picture
Sum up, don't believe the value of Retaincount. (Do not use it to judge)
Usually use arc, also did not notice that there are these problems, since see, right when the understanding.
A good memory is worse than a bad pen! A lot of review of the basic knowledge, one record down or some harvest!
"iOS Learning Basics" Memory management