iPhone Memory Leaks - iphone

If an application produces a lot of memory leaks, are they "just" an in-app problem or are they also in RAM after the termination of the application? So does the iPhone OS release the memory allocated for the sandboxed application?
Thank you

Memory leaks are blocks of memory allocated by the OS for your program to use while it is running, but not correctly returned as not in use when the program has finished with them. So this memory is 'lost'. Your program isn't using it, but the system doesn't yet know that it is free for other use.
When your application finishes running, all of the memory allocated to it by the OS, will be returned for re-use. Which answers your question.
However, memory leaks are a significant bug. On a low-memory device, like an iPhone, the less memory you consume the better, you don't want to be leaking memory as you run. If the device runs low on memory, your application may be terminated or crash, unexpectedly.

Memory leaks occur when you allocate any object and miss out to release that objects while running application , so do analyse in xcode which will help in checking memory leaks, and run profile mode in xcode will help to check leaks possible in application.
and use NSAutoReleasePool to release the autorelease objects which will be created when you just assign objects wothout allocating
hope it helps .

Memory leaks are an in-app problem, but can have side effects on the total available RAM.
They are blocks of memory that are marked in use when they actually are no more used. So they are lost to the app. If you have leaks, this will increase memory consuption. And bad memory usage will be noted by the system and the app might be jettisoned (killed) by the watchdog, jetsam.
So keep your memory leaks to a minimum ;-)
It has an effect on the overall OS, but negligible in consequences. Because your app is not killed when you tap the hole button, but rather "backgrounded", all the memory that's used by your app is still live and unavalaible to the system. Jetsam will first tell you that the memory is low and ask you to get rid of stuff you don't need. Of course you cannot free your leaks.
If you still use too much memory for the system, and it needs to allocate more memory for another process, your app will get killed. All the memory it used will be freed, leaks included.
Leaks are bad, use the static CLang analyzer in Build and analyze.

Related

What's exactly behind / inside iPhone memory management?

This question is NOT about retain/release things in iphone memory management. I understand the routine quite well and there is no memory leak things in my app.
I pop up the question shown in the title, when I use Activity Instruments to monitor the overall memory activity of my app.
The instrument always shows that the amount of "real memory", which my app is using, keeps being between 21 MB and 30MB, never higher. I think this amount is relatively not big. However, sometimes, my app will give level 1 or 2 memory warning (never crash and I don't do anything for this warning in my code).
so I am wondering what's really behind iphone memory thing. I mean, does real memory the only things that triggers warnings? or there is anything else (such as virtual memory, as shown in the Instruments) inside the whole memory I should take care of?
Although my app never crash due to memory issues, this warning thing (especially level 2 warning) really annoys me and makes me fear of crashing once I release it to public in the future.
Any help?
Thanks
Memory warnings exist to tell your app you're nearing your limit. They are not necessarily a 'bad' thing - plenty of applications simply ignore them.
The actual implementation details about when a memory warning is triggered are not important, and in fact will vary considerably from device to device. An iPhone 4 might have 512MB of RAM to play with, but a 3GS will have half that.
That said, there are some things worth knowing about memory warnings:
A memory warning is triggered when the overall amount of available free memory reaches a certain level
These levels are undocumented. So you don't know what the difference is between a level 1 warning and a level 2 warning, other than the fact 2 is worse (more urgent) than 1
Memory warnings are not application specific. A memory warning is delivered to all applications currently running and not suspended. So you may not be directly responsible for triggering one.
When memory warnings are received the system will try and free up memory on your behalf
Again, the exact implementation details are undocumented, and you shouldn't need to care about them. A memory warning is an opportunity for you to help the system by freeing up any objects you don't need.

Why bother to use Memory warning event methods?

If my app uses less than 10MB do I have to bother to use those methods? I know that they are for caching low memory situations but this might occur only if you do not tested you app before releasing it. If you have tested your app, the app does not have leaks, have a small memory footprint, then why should anyone bother to use memory warning methods?
Your app is probably not the only thing running on the device. Other apps and processes are also taking up memory, and in some situations they may need it more than you. It's always a good idea to respond to memory warnings by releasing cached data that can easily be loaded again. That way the operating system has control over the memory usage, and it won't have to terminate your app.

Alternative Tools to find memory leaks in iPhone

Can anybody tell if there are any tools other than CLANG static analyzer and the Memory Leak tools available in XCode.I am developing an application and it crashes after 10 mins of usage due to low memory.I have solved all the leaks pointed out by CLANG and the Tools of Xcode,but still not able to solve the low memory issue.
Thanks
Your application might not be leaking memory. Low memory warnings can also be caused by an application retaining too much memory. If you application is continually consuming memory, it could be that you are retaining references to objects when you shouldn't be. Trying running inside of Instruments and monitor Memory Allocations.
If the problem is that many objects are accumulating in memory, you need to modify your program so that this is not necessary. If they are downloaded, try saving to a file or files. Then you can re-load the objects into memory only when absolutely necessary. The iPhone has a fairly low memory threshold, so applications should be designed with this in mind. How much memory is being consumed? What objects are taking up all the space?

Leaks showing extended memory usage when on iPhone vs Simulator

When, I run my application in the simulator using the Leaks instrument, it uses about 2.5mb of memory. When I run it on the iPhone, it takes forever to launch, slowly climbs to ~34mb of memory and then crashes. However, when I run it on the iPhone without Leaks, it launches quickly and runs fine. Why is this?
Do you have zombie detection enabled?
Zombie detection will cause every object allocated to never be deallocated (the object is marked as a zombie on deallocation). This will cause memory growth as you describe. A common mistake is to leave zombie detection enabled when using Instruments, either through environment variables or through the checkbox in the Object Alloc instrument.
If it isn't zombies or leaks, then it is -- as others have said -- memory being allocated and sticking around. Use the Object Alloc instrument to track the objects allocated in your application and make sure that every single one of them exists for a reason. You can turn on "only track live allocations" to filter out all the objects that have already been deallocated.
The crashes are probably due to the memory leaks you have in your app and the device running out of memory. Without seeing any code it is impossible to tell. Here is a tutorial on how to use instruments
The "takes forever to launch" and and runs slowly is due to the leaks monitoring system polling the device every 10 seconds for information
EDIT: It is probably due to keeping too many objects alive in memory at a given time. Check instruments and Object Allocations. Just because you have no leaks doesnt mean you can't run out of memory

Does iPhone OS reclaim user-space memory at exit?

I'm reading up on the leak-finding tools for iPhone development and intentionally inserting and looking for memory leaks in my small program before I get into a bigger, harder-to-debug project.
It's no problem to identify a memory leak that, say, occurs in a regularly invoked method like responding to a touch event. The leak instrument will eventually identify virtual memory that is unclaimed.
I'm more worried about a leak in code for exiting the entire system. Once I completely exit my app, is it a concern that something wasn't deallocated, or does iphone OS automatically reclaim all user mem at that point?
This issue is unclear to me after reading quite a bit of documentation, and without knowing anything else, I assume it must work like other OS's in that regard and just take back all user space. If that's so, won't I be fine cleaning up regular leaks so my app can run for any amount of time with bounded memory, then not worry so much that everything gets freed up at exit?
Also, if it is critical to free everything before exiting because it will not be reclaimed by the OS, is there a good way to keep my app alive in the instruments after exit for inspection? When I press the home button in the simulator or on the device haven't I already lost the chance to detect exit-time leaks?
Yes, ending your program will release every bit of memory held by it. Anything otherwise is an OS bug, and you're unlikely to find that occurring.
EDIT: I bet your asking this because documentation says "iPhone doesn't support garbage collection." That statement, however, doesn't apply to freeing memory when a program ends. It's only talking about how you have to handle freeing your own memory while your program is running.
The iPhone should be using virtual memory, so (theoretically anyways) the OS will clean everything up when a leaky app exits.