Revolution 2011: Objective-C 3.0, links, garbage collectors

Joseph Vissarionovich, say, the term “hyperlink” would have liked. It’s debatable, and besides, nothing to do with what this article has. It will focus on a completely other links. And other entities-called the above words… In 2011 to replace the memory management system that was invented 20 years before this great Bud Tribble (one of the creators of the first Mac, the head and author of NeXTSTEP, then – senior Vice President, Sun Microsystems), came new.

The old and the new coexisted for a long time (don’t know whether it was appropriate here past tense), but it was one of those rare occasions when new was so much better and easier that even in its original and imperfect view of the ARC almost instantly became the primary.

Not everything was rosy – a new memory management system makes life easier, but brought with it a new problem.

ARC was developed by the LLVM team, under the direction of Chris Later. At the same time, the same team that was involved in project in the status of a hobby, thus “Objective-C 3.0 C”, known today under a different name. In the same year, the compilers based on LLVM 3 finally ousted from the Arsenal of the GCC of the company.

I had the privilege to chat with Chris, about that time – if I had the opportunity to choose Apple’s CEO, I would choose Forstall or Scott, or Chris Latner – but rather both of them, and Jonathan Ive to help them, for balance and internal competition…

This is a continuation of a series about WWDC 2011, previous parts here:
First part: WWDC 2011: Apple takes to the clouds…;
Second part: WWDC 2011: About the murder;
Third part: iCloud, on the other side of the screen;
The fourth part: memory Management and garbage collectors.

The traditional system of memory management

System Tribble (so no one calls, but we will call a) based on counting the number of references to the object.

In the first versions of NeXTSTEP that was not what caused the problem. An object created in one program could be used in several different places and could easily be removed in one of these places, becoming a dummy, try to access which will inevitably lead to program crash.

Do not remove items even worse: many of them take up much space in memory, as well as the language, and NeXTSTEP lived a dynamic and unpredictable life in advance (this was one of their important advantages), this practice led to the overflow of memory, and the same crash.

System memory management based on reference counting was not invented bad. He invented that for nearly 20 years has transformed the lives of converts to the torture, but in skilled hands it was fantastically effective – autorelease method. Only it happened a little later.

In the beginning there were two teams, retain (hold) and release (release). And objects have a reference count. At birth, his counter was assigned a value of 1.

The retain command was added to the value of 1, release reduced the value of the same unit.

Now if born at point A, the object is passed to B, the receiver sent him a message, retain, increasing the counter value by one. If this object is no longer needed, he was sent release. Now, anywhere where the object is used, it is guaranteed to be kept alive until needed.

After zeroing the counter object is destroyed. Team autorelease added to the system dynamism, allowing you to create temporary objects without worrying about their destruction, they were killed themselves (after exit from the context).

Like everything else, these objects were created with the unit on the counter, but other than that, the link to them was placed in the hit list, in NSAutoreleasePool. In each revolution of the cycle control program is sent to all objects in the list, the release message and cleaned it up.

If the programmer followed the rules to point B has arrived, the objects entered in the List, if the object needed for a short time (to know time and display it on the screen), he quietly and independently “died”. If for long – he went retain. Actually all. This is the impenetrable obstacle. Rules and conventions were more meticulously all of them can be difficult – required training.

The garbage collector

How works the garbage memory it is easy to guess. He remembers where all the objects, said references to them, and if the object is no longer needed, on their own schedule, starts the cleansing process. All of this is happening simultaneously with the operation of the program, it takes resources, memory and CPU cycles.

But the programmer doesn’t need to worry about anything – spawn-and-forget.

Is the process execution time, an extra load, and so on – alas, in iOS devices where this is particularly unacceptable, the garbage collector is inappropriate. But what about Android, where Java and the garbage collector “on the job”?

Automatic reference counting (ARC)

Scratch, from scratch, write something similar to the ARC would be impossible, but in the group of low-level (LL!) technology office of development tools Apple, in the framework of the project for the transition to LLVM-compilers, was developed by the Xcode Static Analyzer.

Its first version we called “bad advice” – it is often wrong. But with each version he became wiser, and already was a rule: if you and analyzer diverge in opinions on some occasion, think again: 9 out of 10 he’s right. If not 99 out of 100.

ARC does the same thing for 20+ years did everything to write software for NeXT, Apple Cocoa and Apple’s Cocoa Touch – only inhuman pedantry. And at compile time, just like the men before him, ARC generates the code for memory management.

The memory management is based on the same principle – on the reference counting. ARC like magic, but it is not magic. When people decide in what form to give the object to the outside world (sentenced or not), it is based on common sense and knowledge of life. ARC from either the first or second – no.

The agreement reflected the longevity of the returned objects that had existed for fifteen years, are much more important, they had to clarify: if we don’t help at ARC, no one will help him.

If a method name begins with one of four combinations of letters, after which a capital letter is a method that returns the long-lived objects. Otherwise – no.

Four magical blend: new, alloc, copy or mutableCopy.

Use the retain, release and autorelease was… forbidden.

Tools for memory management has been re-designed, more efficient, and it was used only ARC with inhuman accuracy.

There were downsides: ARC worked with the code only in Objective-C. Programs for the Mac and for iOS devices are composed of fragments in C, and accesses to memory regions created in different libraries – they are not supported, to interact with them required special measures, that is extra time. And that was another reason for errors.

ARC developers have put a lot of effort to ensure compatibility with the code without ARC support. Almost got it – although the problems happened.

Problems, however, was surprisingly little – even though it was the first version of the technology, radically changing the foundations.

To be continued…

We offer you to subscribe to our channel in “Yandex.Zen”. There you can find the exclusive materials that are not on the website.

Leave a Reply

Your email address will not be published. Required fields are marked *