views:

421

answers:

8

I've only used Java and Ruby for so long that I'm really not enjoying keeping track of my own memory again. It's not that I can't do it, I can. I just don't want to.

Any special tricks, libraries, or anything else you've learned for dealing with memory in iApps, lay it on me here.

+2  A: 

It's a pain. I'd say the most important habit to get into is running the memory tool after you write every significant piece of code, at least until you get a feel for how things work.

And be sure to run it on the device, not the simulator. The two handle some memory situations differently.

Also, don't be surprised if you see memory usage go up when the system is caching an asset. If I recall correctly, I spent a lot of time trying to figure out why a bitmap wasn't going away. I didn't have my talons in it anymore, but the OS thought it might be a good idea to cache it until memory ran low.

Here's a great blog post on how icky iPhone memory management can be. Some good ideas in there.

Nosredna
A: 

I haven't done this, but I know that Mono has been ported to the iPhone, which means that you can write iPhone applications in .Net (C#, VB.Net etc.), and not have to worry about memory management.

MusiGenesis
I'd be surprised if it's that easy. I'd suspect that an app running IL code would fall foul of Apple's "no interpreter but the ones we supply" rule. But if you're not aiming for selling on iTunes, I guess it doesn't matter.
Nosredna
according to mono website, they have gotten apps approved in the apple app store. I'll upvote toget rid of the downvote
Jacob Adams
This might have been the stupidest downvote I've ever received. Mono has already been ported to the iPhone, and .Net applications have already been written for it (although I've never done it personally), so we already know that it's possible.
MusiGenesis
Thanks, Jacob. Jeez Louise.
MusiGenesis
unfortunately, this is closed source at the moment and not very accessible. I think running garbage collection inside a ported .NET environment would be worse than running it in Objective-C, though! I imagine performance would be pretty lousy.
Ben Gotow
Does the C# compiler target the ARM rather than IL virtual machine?
Nosredna
They say "No iPhone APIs have been exposed, so you get the very basic foundation." So it makes sense for Unity to have Mono be part of the system. So I'd say this is great for someone who wants to use Unity, but probably not what you want to do if you're planning to use iPhone APIs.
Nosredna
@Nosredna: again, I have no experience with this, but my understanding of mono is that its implementation of InteropServices allows .net applications to access any OS-specific APIs, although this of course would break the cross-platform ability of your app.
MusiGenesis
@Ben Gotow: I don't know how well mono apps perform on the iPhone, but there have been some apps approved, so it must be at least OK.
MusiGenesis
Yes. Mono apps are on the app store. All apps written using the Unity game engine for iPhone run in Mono on the iPhone. There are several hundred of them on the store. This doesn't make it a good idea to use .NET for your iPhone development. The problem is, "porting" the runtime doesn't mean that you can create an interface. You'd have to create all your own interface elements from scratch (no UIButton, UITableView, etc...) Unless you're writing a game, you'd probably end up with some ugly, nonstandard mess.
Ben Gotow
Is there anything in the store based on Mono besides Unity games?
Nosredna
@Ben: mono doesn't provide UI elements? I think I need to start actually using mono, since I'm planning to rely on it to accomplish a windows-to-mac port of my app. I've been mulling over the wisdom of creating my own OS-independent UI entirely (iTunes is an exceptional example of how bad an idea this is), but if I have to I have to.
MusiGenesis
Wow. 2 downvotes for a perfectly legitimate answer. SO rox!
MusiGenesis
+5  A: 

It's really very, very easy. Stop worrying about retain counts and, for goodness sake, never call the "retainCount" method on an object. It's useless.

If you call a method with "alloc", "new", or "copy" anywhere in its name, or you call the "retain" method, you own that object. You're responsible for relinquishing that ownership when you're done with it.

To do that, call "release" if you don't still need to return a reference to the object or "autorelease" if you do.

The documentation on the subject is unequivocal. It's not difficult.

Jim Puls
this is good practice in the basic case, but calling retain doesn't mean you "own" the object - since many objects may call retain and "own" it as well. It just means you're using it and don't want it to go away. This is why retainCount is important!
Ben Gotow
As long as each object that calls "retain" also calls "release" when it's finished with the object, retainCount is a pretty useless stat. Release does not dispose of an object, it simply disposes of an object's handle to that object. When all the handles are gone, then the object is dealloc'ed.
mmc
I never said you're the only thing that owns the object.
Jim Puls
Jim is spot on - calling "retain" does mean (in a memory management sense) you own the object. It means the object will persist until you release it. retainCount is entirely useless, if you ever feel like using it, even for debugging, don't. Track retain/release/autorelease, using conditonal breakpoints, bt, and continuing breakpoints.
Peter N Lewis
In this sense, think of group ownership. A perfect example is owning a timeshare. If you have a stake in it, you're responsible until you explicitly unload your responsibility. The retain count itself is critical, true, but calling "retainCount" to check the value is pointless. If you're following the rules you don't need to care what the retain count is — that's the runtime's job to manage.
Quinn Taylor
A: 

They just had a Dot Net Rocks show on this

http://www.dotnetrocks.com/default.aspx?showNum=454

They mention both the mono project and the fact that Objective C 2.0 will have better memory managemnet capabilities. Other than that, it sucks.

Edit:fixed bad typos

Jacob Adams
It took me a surprisingly long time to figure out that I was supposed to listen to something at that link. I didn't realize I've become so text-based.
MusiGenesis
Just as an aside, Objective C 2.0 is already available and the iPhone SDK uses it. It has a pretty nice garbage collector, but it's disabled in iPhone apps for performance reasons.
Ben Gotow
A: 

It can be awkward coming from Java or Python (I did), but once you get used to it, it's just natural. You own objects created with alloc or copy and must release them, anything else not. Most of the time you will be creating autoreleased objects (collections, strings).

Marco Mustapic
+2  A: 

It's not really that difficult. Here's some rules of thumb that will go a LONG way.

  • Release all properties in the dealloc (unless they are set to "assign").
  • If you "alloc" "new" or "copy" in your initialization, you probably need to "release" in the dealloc.
  • If you "alloc" "new" or "copy" at the beginning of a method, you probably need to "release" at the end of the method.
  • If you "alloc" "new" or "copy" something to put in an Array, Set, or Dictionary, go ahead and release it... the collection will retain it without any help from you. (I am not a big fan of autorelease, but this is a pretty decent situation for its use. Create the object autoreleased, then add it to the collection. Done.)

These are really rough rules of thumb, and I'm sure everyone can come up with exceptions to these rules, but these will go a long way toward getting you allocating and releasing memory the right way.

For the rest of the way, run your code with the Leaks instrument often. Fix problems AS YOU FIND THEM. It's much easier to fix it now than it is to fix it in a month, when you don't remember why you wrote the code that way.

And finally... it's rare, but Apple code DOES sometimes leak. The Leaks instrument should tell you exactly what is being leaked. If it doesn't look like something you are doing, it's POSSIBLE that it's not. As a concrete example: Core Data for the iPhone leaks small amounts of RAM upon (I think) creating a new ManagedObjectContext. Might be when you do a fetch, though, I can't remember right off the top of my head.

mmc
+3  A: 

I would highly recommend getting Beginning iPhone Development: Exploring the iPhone SDK.

Another key thing to look into is getting the Clang Static Analyzer setup. It analyzes your code and can catch many common memory leaks as well as potential logic issues. Apple has contributed to this analyzer heavily and will be looking to integrate it into future releases of X-Code ;)

Jab
The Clang Static Analyzer is an astounding tool for seeking out memory leaks, as well as other code errors. Because of the way it tests all branches within your code, it can find things that you might never have seen. It also provides extremely descriptive breakdowns for where you are leaking memory and why.
Brad Larson
+1  A: 

Forget all this, don't bother reading anyones comments/answrers, just read the very simple Memory Management Rules. Its only a dozen sentences for heavens sake, you can read it in one minute.

Peter N Lewis