Cocoa (API) - Memory Management

Memory Management

One feature of the Cocoa environment is its facility for managing dynamically allocated memory. Cocoa's NSObject class, from which most classes, both vendor and user, are derived, implements a reference counting scheme for memory management. Objects derived from the NSObject root class respond to a retain and a release message and keep a retain count which can be queried by sending a retainCount message. A newly allocated object created with alloc or copy has a retain count of one. Sending that object a retain message increments the retain count, while sending it a release message decrements the retain count. When an object's retain count reaches zero, it is deallocated similar to a C++ destructor. dealloc is not guaranteed to be invoked.

Starting with Objective-C 2.0, the Objective-C runtime implements an optional garbage collector. In this model, the runtime turns Cocoa reference counting operations such as "retain" and "release" into no-ops. The garbage collector does not exist on the iOS implementation of Objective-C 2.0. Garbage Collection in Objective-C runs on a low-priority background thread, and can halt on Cocoa's user events, with the intention of keeping the user experience responsive.

In 2011, the LLVM compiler introduced ARC (Automatic Reference Counting), which replaces the conventional garbage collector by performing static analysis of Objective-C source code and inserting retain and release messages as necessary.

Read more about this topic:  Cocoa (API)

Famous quotes containing the words memory and/or management:

    Language was not powerful enough to describe the infant phenomenon. “I’ll tell you what, sir,” he said; “the talent of this child is not to be imagined. She must be seen, sir—seen—to be ever so faintly appreciated.”... The infant phenomenon, though of short stature, had a comparatively aged countenance, and had moreover been precisely the same age—not perhaps to the full extent of the memory of the oldest inhabitant, but certainly for five good years.
    Charles Dickens (1812–1870)

    The care of a house, the conduct of a home, the management of children, the instruction and government of servants, are as deserving of scientific treatment and scientific professors and lectureships as are the care of farms, the management of manure and crops, and the raising and care of stock.
    Catherine E. Beecher (1800–1878)