Objective-c Quick Start Tutorial (iii)

Source: Internet
Author: User

See this article as you have learned the basics of object-oriented.

(I think this theory of the concept of something, no one teacher will understand when they say it.)

Here does not mention my teacher how NB, he said several times I did not understand, may be I stupid.

We still understand, the main or more to knock the code, so it is more easily understood why object-oriented)

Now we're going to introduce a series of OC descriptions of memory.

Before you also see in the first piece of article. @autoreleasepool

This thing is in Xcode4.2 and later versions due to the introduction of the arc (Automatic Reference counting) mechanism.

Same as the GC function in Java.

But it's not the same thing, and arc is the compile-time compiler "helping you" insert the memory management code that originally needed your handwriting.

rather than a garbage collection system that runs like a GC. Instead , Xcode can automatically add memory release code to your code when the program is compiled.

But there is no garbage collection mechanism in OBJC, so how is memory managed in OBJC?


In fact, the management of memory in OBJC is dependent on the object reference counter:
In OBJC, there is a corresponding integer (Retaincount) inside each object.
Called "Reference counter", when an object is created, its reference counter is 1,
When calling this object's alloc, retain, New, copy method, the reference counter automatically adds 1 to the original base.
(The method of invoking an object in OBJC is to send a message to the object),
When the release method of this object is called, its reference counter is reduced by 1,
If an object has a reference counter of 0, the system automatically calls the object's Dealloc method to destroy the object.

So as long as Alloc init after the unused objects must be released otherwise it will cause memory leaks.

Java's memory is a more complex memory-freeing mechanism.

A simple introduction to the Java memory, any Java virtual machine does not have a similar cleanup method.

The idea of a Java virtual machine is that all objects can be traced back to the surviving stack or static storage area,

All the live objects can be found by traversing all references. Each reference to the discovery must be traced to the object it refers to,

Then there are all the references that this object contains, so repeat. Until the source stack and the static zone form the network are all accessed.

In this way, there is an adaptive garbage collection technology. How to find the surviving objects depends on the different virtual machines.

There is a kind of hair method called stop-copy . It means pausing the program first (it's not a background garbage cleanup) and then

They are all copied into a newly detached heap of memory. All objects of the original heap memory are then deleted.

It's slow to start (two reasons). 1 is to have two heaps 2 is two heaps back and forth toss) when the program slowly stabilizes.

will be converted to markup-cleanup

The idea of "tag-sweep" is also based on a stack and a static storage area, traversing all references to find all the surviving objects.

Whenever it finds a surviving object, it sets a tag for the object, and the process does not reclaim any objects.

The cleanup action will only begin when all marks are completed.

During the cleanup process, objects that are not marked are freed and no copy actions occur.

So the remaining heap space is discontinuous, and the garbage collector will have to rearrange the remaining objects if it wants to have contiguous space.

Think of Java Memory management mechanism is high-end, worry.

Now that we know about the memory-recycling mechanism of OC and Java, we have to discuss the object of initializing the contents of the allocated memory.

initialization keywords:new and alloc init

New is seldom used in real-world development, and it is common to create objects we see all [[ClassName alloc] init]

What is this for?

I don't know if you've found it: if you use new, the initialization method is fixed and can only call Init. Cannot invoke other initialization methods that we have prepared

This is very inflexible.

But there are other differences besides this default initial. The answer is, of course.

With the source code, we'll look at their differences.


+ NEW
{
ID newObject = (*_alloc) ((Class) self, 0);
Class Metaclass = self->isa;
if (class_getversion (Metaclass) > 1)
return [NewObject init];
Else
return newObject;
}

And alloc/init like this:
+ Alloc
{
Return (*_zonealloc) ((Class) self, 0, malloc_default_zone ());
}
-Init
{
return self;
}

The difference is that the zone is used when alloc allocates memory.
What is this zone?
It is the time to allocate memory to the object,
Assigns the associated object to an adjacent area of memory,
The process speed of the program is improved by the cost of easy call.



Objective-c Quick Start Tutorial (iii)

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.