From Alloc and Allocwithzone in Objective-c.

Source: Internet
Author: User

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:
    1. static Mygizmoclass *sharedgizmomanager = nil;
    2. + (mygizmoclass*) Sharedmanager
    3. {
    4. if (Sharedgizmomanager = = nil) {
    5. Sharedgizmomanager = [[Super Allocwithzone:null] init];
    6. }
    7. return sharedgizmomanager;
    8. }
    9. + (ID) Allocwithzone: (Nszone *) zone
    10. {
    11. return [[Self sharedmanager] retain];
    12. }
Copy Code
which
    1. Sharedgizmomanager = [[Super Allocwithzone:null] init];
Copy Code
there is another version of this paragraph that does not use allocwithzone but directly alloc, as follows:
    1. 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
  1. Nszone
  2. Used to identify and manage memory zones.
  3. typedef struct _NSZONE Nszone;
  4. Availability
  5. Available in OS X v10.0 and later.
  6. Declared in
  7. 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.

From Alloc and Allocwithzone in Objective-c.

Related Article

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.