Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Saturday, October 16, 2010

#VTM_iPhone notes on memory management talk

Here are my notes on Steve Kochan's talk on "Really, really understanding memory management"

Steve@classroomM.com
Twitter: @skochan

Founder of classroom.com, author

Objectives: to really understand different memory maNagement strategies, et al

Three memory management strategies: automatic garbage collection, autorelease pool, manual allocate. And release of objects

Garbage collection not avail on ios-

RAM space ranges from 128-512 mb on ios objects, but count on much less available. There's no memory paging.

Autorelease pool is created at start of main and drained at the end of main

For cocoa and ios apps, automatically drained at end of event cycle

Multithread apps need separate pools per thread.

Framework objects are (mostly) autoreleased.

Doing example with a fraction class too much to take notes here.

Alloc involves retain - must be released.

If you release the single retained object twice (over released) error message says "released freed object"

Dealloc - never called directly, of course.

Retain count: how many people have dibs on this object?

When retain count hits zero, system invokes dealloc...

Autorelease doesn't affect retain count, but when pool is drained, the object will be released (once). The "pool" isn't a space where objects are stored - it is a list of objects that should be released when you drain the pool.

When is the pool released? Other than when you specifically tell it to? At end of event loop.

Retaining allows objects to survive after the pool is drained... From one event loop to the next.

When you add an object to an array, it sends a retain to the object. True for most methods that have "add" or "replace" in method name.

Methods with "remove" in name will release.


You can have local autorelease pool, even nested ones.


Alloc, copy*, or new must manually maintain

Autoreleased methods: static methods that return objects e.g. [nsmutabledictionary dictionary] returns an autoreleased object.
[[nsmutabledictionary alloc] init] is not autoreleased. Both have been retained once.

Trouble example: add a view to a superview and release it. Later in program, remove from superview - the view is deallocated.

Strange optimizations: constant string objects and nsnumber objects from -1 to 12 are often higher than they "should" be.

If you don't need an object in a method but are going to return it, now is a good time to autorelease.

Event cycle:

Remember that a new autorelease pool gets created when your app is to process an event. The pool gets drained when that method is done. Event is pressing a button, dragging, etc.

Example of trouble: in viewdidload method, user has said data = [nsarray array] -- it gets deleted at end of view loading event!

Could retain, could use alloc/init, or use synthesized setter method, which retains. ****But don't want to alloc AND use synthesized setter- that double retains! Causes a memory leak****

If you use @property (retain), then setter involves retaining. @property(assign) doesn't retain.

Another example: when you initWithCoder, the stuff in the coder is autoreleased, so you better do something that retains the data!

@property (copy) always makes an immutable copy, not a mutable one, even if the instance variable is of a mutable type.

When memory gets low in iPhone:
Application did receive memory warning
Release data you don't need!

Didreceivememorywarning is sent to all view controllers.
By default, views will be released if not in a superview
When view is released, viewdidunload is sent to the viewcontroller


Finding memory leaks
Put breakpoint or slog in dealloc

Use static analyzer- build and analyze puts blue icons in bak point bar. Click on blue boxes for more and more helpful info! Very neat!

Instruments - get guide from apple's website

More resources on PowerPoint that Steve posted.

- Posted using BlogPress from my iPad

No comments:

Post a Comment