Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which iPhone OS memory management rules and how-to's do you know?

Currently I am jumping into the ice cold water called "memory management in iPhone OS".

Here's one rule i've learned:

Every time I see an alloc in my method, I will release that corresponding variable at the bottom of the method.

Every time I create an @property(...) in my header file which says copy or retain, I put a release message on that variable into the dealloc method.

Every time I have an IBOutlet, I do the same thing. Only exception: If the IBOutlet has something like @property(... assign), or in other words: If it has the assign keyword at all. Then I don't care about releasing it in the dealloc method.

I feel that there are many more good rules to know! Just write down what you have. Let's scrape them all together. Links to great descriptions are welcome, too.

like image 568
Thanks Avatar asked Apr 24 '09 16:04

Thanks


People also ask

What is memory management in iOS?

Memory management is the programming discipline of managing the life cycles of objects and freeing them when they are no longer needed. Managing object memory is a matter of performance; if an application doesn't free unneeded objects, its memory footprint grows and performance suffers.

What is memory management in iOS Swift?

Swift uses Automatic Reference Counting (ARC) to track and manage your app's memory usage. In most cases, this means that memory management “just works” in Swift, and you don't need to think about memory management yourself.

How do I check memory usage on my iPhone?

Go to Settings > General > [Device] Storage. You might see a list of recommendations for optimizing your device's storage, followed by a list of installed apps and the amount of storage each one uses. Tap an app's name for more information about its storage. Cached data and temporary data might not be counted as usage.

What is Objective C memory management?

It is the process by which the memory of objects are allocated when they are required and deallocated when they are no longer required.


1 Answers

Actually, any time you initialize an object and the method name includes "init" you are responsible for releasing it. If you create an object using a Class method that does not include the word "init" then you don't.

For example:

  NSString *person = [NSString stringWithFormat:"My name is %@", name];

does not need a release. But:

  Person *person = [[Person alloc] init];

needs a release (as you stated in your question). Likewise:

  Person *person = [[Person alloc] initWithName:@"Matt"]];

also needs a release.

This is a convention, not a rule of the language, but you will find that it is true for all Apple-supplied APIs.

like image 121
mmc Avatar answered Oct 19 '22 06:10

mmc