Original address: cachecrew.com/memory-mana…

cachecrew.com/

Release date: May 2011

Proper application memory management is the key to application stability. If done incorrectly, a million bad things can happen! Side effects include destroying application data, crashing applications, locking up the use of computers or devices, and even crashing or causing system crashes. Side effects may include corrupting application data, causing the application to crash, locking up the use of the computer or device, or even crashing or causing the system to restart. As a result, memory management issues are always on every programmer’s mind (and, unfortunately, on their mind), especially when they’re learning a new language. This article covers programming issues related to memory management. In the next article, I’ll cover more of the best memory management practices in iOS/CocoaTouch view controller programming to be more technical.

In older programmatic programming languages that allowed memory allocation (non-object-based languages such as C or Pascal), managing memory was straightforward.

int appFunction(int size)
{
int *array = malloc(size * sizeof(int)); ⋮ free (array);// Done with the dynamically allocated memory
} // appFunction()
Copy the code

Example. C language memory allocation.

In object-oriented programming, application designers tend to structure their designs as fluid interactions of objects. The syntax of object-oriented languages such as C++ or Objective-C helps programmers avoid memory management errors more easily than procedural languages, especially in more complex application designs.

Because Objective-C was invented with apis and frameworks used by environments such as iOS and Mac OS X, Apple assumes that its use has a hierarchy of classes, and that, for all practical purposes, it relies on the NSObject base class. This class determines the rules of the game and is almost indistinguishable from the Objective-C language itself, so without NSObject it’s hardly worth discussing the use of Objective-C. Although it may seem complicated, it is important to note that assignment and de-assignment are handled at well-known locations in the code that will be used for other purposes, initializers and deinitializers.

- (id)init:(int) size
{
self = [super init];
if (self)
_array = [[NSArray alloc] init];
return self;
} // -init
⋮
– (void)dealloc
{
[_array release];
[super dealloc]; // Done with the dynamically allocated memory
} // -dealloc
Copy the code

Example: Objective-C memory processing

Just as in C you need to internally determine when malloc() and free() should be called, in Objective-C you need to internally determine “object ownership” and when to send hold and release messages. These hold/release messages (sending messages is similar to function or method calls in other languages) implement reference counting — a way of keeping track of how many objects are referencing that object.

Internalize this rule: If you allocate, retain, or copy an object, you own it, and your job is to release it…… Otherwise it’s not.

In Objective-C, an object allocates other objects for its use or for the use of other objects. These allocations are memory allocations. Cocoa (used in Mac OS X) and CocoaTouch (a Cocoa variant used in iOS) programming frameworks assume a clear understanding of who owns an object. Any number of objects can retain ownership of another object, either explicitly or implicitly. If an object is assigned to another object, that object has an implicit ownership.

StringOwner implicitly owns the assigned object, so it must release it.

NSString *stringOwner = [[NSString alloc] init];
⋮
[stringOwner release];
Copy the code

The tempString does not acquire ownership, but it can use it for the duration of the method.

NSString *tempString = [NSString string];
Copy the code

StringOwner had already claimed ownership through Retain, so it had to release it.

NSString *stringOnwer = [[NSString string] retain];
⋮
[stringOwner release];
Copy the code

If an object is passed as a parameter to another method, then another method might ask to own the object. In this case, the sender (that is, the “caller” in other languages) can release the object later when it is no longer needed.

NSString *stringOnwer = [[NSString string] retain]; ⋮ [existingArray addObject: stringOwner];// array is also, now, an owner
⋮
[stringOwner release]; // Not needed by this method (or class any longer)
Copy the code

The problem is that while Apple has a simple set of rules (or, better yet, rules of thumb), it’s often unclear which of their apis retain ownership of incoming objects, even when reading official documentation. I usually find that if I’m not sure, I need to do a Google search — which tends to lead to the most reliable advice from Stack Overflow.

Apple must know this because they provide their excellent (though imperfect) Instruments tools that work with their Xcode development environment to help find memory errors and leaks.

Next, I’ll write about memory usage as it relates to the iOS CocoaTouch view controller (because that’s also pretty obvious).

Resources – Basics

  • Application program interface
  • Programmatic programming languages
  • Object-oriented programming language
  • Reference counting
  • Software programming framework.
  • Apple’s memory management programming guide. Or better yet, memory management, rules of thumb.
  • Stack Overflow! High quality user contribution help forum.

Translation via www.DeepL.com/Translator (free version)