SolutionIOS CodingProcessMemoryThe issue of allocation and release is described in this article.MemoryHow to allocate and release data.
InIos ProgrammingMemory operations are not required. When I first came into contact with ios programming, the program crash is always caused by such memory problems. In fact, the release of memory in ios programming is relatively simple. You only need to release or autorelease your own retain, new, alloc, copy, and mutableCopy objects. One principle is: If you allocate storage, you are responsible for release.
In addition, you should note that many methods will automatically retain the added objects, such:
- NSString * test = [[NSString alloc] initWithFormat: @ "% d", 111];
- NSLog (@ "% d", [test retainCount]); // The retain of test is 1 at this time
- NSMutableArray * array = [[NSMutableArray alloc] initWithObjects: test, nil];
- NSLog (@ "% d", [test retainCount]); // at this time, the retain of test is 2. Because the test object is added to the array, test is retain.
- [Array addObject: test];
- NSLog (@ "% d", [test retainCount]); // at this time, the retain of test is 3. Because the test object is added to the array, the test will be retain.
- [Array release];
- NSLog (@ "% d", [test retainCount]); // at this time, the retain of test is 1, and the array is released. It Automatically releases its internal objects, so the retain count of test is changed back to 1.
So after calling a method like addObject: test, if you are not using the test object, release it without worrying about "if I release test, so will the test in array be lost?" If you don't release it, it will leadMemory.
RetainCount is similar to the reference count in java. When retainCount is 0, the dealloc method of the object is called to release the object.
Conclusion: SolutionIOS ProgrammingMediumMemoryThe issue of allocation and release has been introduced. I hope this article will help you!