First, the origin of the problem
All originated in Apple's official documentation for the sample code for Singleton (Singleton): Creating a Singleton Instance. The main controversy is focused on the following paragraph:
- static Mygizmoclass *sharedgizmomanager = nil;
- + (mygizmoclass*) Sharedmanager
- {
- if (Sharedgizmomanager = = nil) {
- Sharedgizmomanager = [[Super Allocwithzone:null] init];
- }
- return sharedgizmomanager;
- }
- + (ID) Allocwithzone: (Nszone *) zone
- {
- return [[Self sharedmanager] retain];
- }
Copy Code which
- Sharedgizmomanager = [[Super Allocwithzone:null] init];
Copy Code there is another version of this paragraph that does not use allocwithzone but directly alloc, as follows:
- Sharedgizmomanager = [[Super alloc] init];
Copy Code This leads to a discussion, why to cover the Allocwithzone method, exactly what is the difference between alloc and allocwithzone?
Second, Allocwithzone First we know that we need to make sure that there is only one instance of the Singleton class, and when we initialize an object, [[Class alloc] init] actually does two things. Alloc allocates memory space to an object, Init is the initialization of the object, including setting the initial value of the member variable. In addition to the Alloc method, there is another way to allocate space to the object: Allocwithzone. In the nsobject of this class Official Documents inside, the Allocwithzone method describes that the parameters of the method are ignored, and the correct procedure is to pass nil or null parameters to it. The reason why this method exists is a legacy of history.
Do not
override allocwithzone:to include any initialization code. Instead, class-specific versions of Init ... methods.
This method is exists for historical reasons; Memory zones is no longer used by objective-c.
It is mentioned in the document that the memory zone has been deprecated, only for historical reasons to retain this interface. I didn't find out what the historical reason was, but the content of the introduction would be a little more involved. The practice proves that when initializing an instance of a class using the Alloc method, the default is to call the Allocwithzone method. The reason for overwriting the Allocwithzone method is obvious: to keep the singleton instance unique, you need to overwrite all methods that generate a new instance, and if someone initializes the singleton class without walking [[Class alloc] init], it is directly Allocwithzone, then this single case is no longer a singleton, so it is necessary to plug the method. Allocwithzone's answer to this is solved, but the problem is endless. Here's another question: What's the hell is Memory Zone?
Third, Nszone Apple's Official document contains a few simple words, stingy: References
- Nszone
- Used to identify and manage memory zones.
- typedef struct _NSZONE Nszone;
- Availability
- Available in OS X v10.0 and later.
- Declared in
- NSZone.h
Copy Code
The main idea is that Nszone is a way that Apple allocates and frees memory, not an object, but rather uses a C structure to store information about the memory management of the object. Basically, developers don't have to bother with this stuff, cocoa. Application uses a system default Nszone to manage the application's objects. So when do you want to have a nszone of your own control? When a large number of objects are managed in the default Nszone. At such times, the release of a large number of objects can lead to serious memory fragmentation, cocoa itself has been optimized, each time the alloc will try to fill the memory gap, but it is expensive to do so. Thus, you can create a nszone yourself, so that when you have a large number of Alloc requests, it is all transferred to the specified Nszone, reducing the amount of time overhead. In addition, using Nszone can also be in one breath to remove the contents of the zone you created, eliminating a lot of time to dealloc objects. In general, when you need to create a large number of objects, you can save some time by using Nszone, but only if you know how to use it. Timothy also said that if you can use Nszone, multiple objects at the same time alloc can reduce paging use, and at the same time Dealloc can reduce memory fragmentation. Presumably later Apple has done this, transparent to developers, without the need for developers to do it themselves.
Iv. Conclusion Allocwithzone is not encouraged by Apple, and most of the time programmers do not need to manage their own zone. Of course it's always nice to know more about something.
|