I'm reading some people stating that if another (3rd party) app on someone's iPhone has been leaking memory, that this may reduce the (mystery) amount of RAM your app would otherwise have available.
This confuses me -- does not all app memory get released when the app is closed by the user? And only one app is open at a time on iPhone?
Normally, any memory that your application allocates will be freed when it exits. However, many of Apple's applications continue running after they're "closed", so memory leaks in Mail, for instance, can affect available memory.
In addition, there are apps out there that claim to free up allocated memory. They really don't do anything other than force some dirty pages out of the buffer cache, but they appear to do something, so people believe they must be doing something useful.
On a jailbroken phone - yes, third party apps can be running at the same time as yours. Running out of memory is common with people who like to have many apps running at once hence the need for task managers, killing tasks etc.
On an unhacked phone - no. Yours is the only non-apple app that is running, no others can run at the same time.
So what can you do? All you can do is try to use the minimum memory possible which you're probably already doing. Realistically you can only test with a factory, unhacked phone, unless you are going to spend hours trying to please everyone. If you think you are maybe using too much you could identify the larger allocations using the instruments tools ("Run with performance tool >" from within Xcode) and then post that chunk of code here to get ideas of how to reduce it.
You should run Instruments and then add the instrument "Memory Monitor" to see the memory use of all of the other processes on your phone. (Add with Window -> Library , then drag the Memory Monitor instrument to the instrument panel.
What I'm still trying to determine is why is iOS releasing memory from MY app, and not all of the other memory pig apps that are not currently running.
Related
I am developing an Application for iPad & it consumes 5.5 MB at max on RAM of the iPad. I have ensured all this stuff using Instruments number of times. As soon as App uses little bit more memory, It throws memory warnings & app start behaving abnormally.
I have implemented the lines of code for freeing the memory in did-receive memory warning.
I also have ensured that NO LEAKS in the entire app.
I came to know that App needs at-least 6 mb memory.
Here is the general flow of the app.
log in -> 1st-list View -> 2nd-list view -> 3rd-list view -> 4th-Map & listview -> 5.Map&List
brief of each screen.
log-in - simple screen with username-pswd, making a web-service call & authenticating user
based on result of authentication, it will fetch few more data & list it on 1st-list View screen.
based on selection from 1st-list, app will load list on 2nd-list view
based on selection from 2nd-list, app will load list on 3rd-list view
based on selection from 3rd-list, app will load list & map will have pins relatively.
based on annotation from Map, app will navigate to another maps & different pins.
Here, app receives memory warnings on 6th screen. I have ensured all coding standards & all importance of all objects.
In Above case, All screens are very necessary, I am using Navigation Controller & just pushing view-controllers one by one. no duplications - ensured twice.
Now, the point is, App is being tested on iPad-1 with iOS 4.1 & all apps are forcefully closed before starting the testing of the app.
THOUGH, app receives warnings # just 5.5 mb usage.
I tried to convince my project managers that, this amount of memory usage is high & they are asking me - there should some option to set MEMORY. How this kind of High Graphics games & apps are running smoothly ?
I can't find appropriate document from Apple saying LIMIT. I have no answer to them.
If setting Memory limit is not possible, I am requesting you to provide Appropriate PROOF link, so that I can show it to my Project Manager.
If that kind of settings is possible, I am highly eager to know how to make that.
I doubt you will find any positive statement about memory limits for an app under iOS coming from Apple docs.
But if you google "ios app available memory" you will find a lot of information; e.g., this S.O. thread.
In my experience, my apps can sustain a "peak" memory usage of about 20MB (depending on the device state), but then memory use must quickly go well below the 10MB threshold for the app not to be killed (I would say that 6-8MB is more precise figure). Sometimes an app can be killed just because a peak cannot be "undone" fast enough.
One piece of information that might be useful for your managers is the output from the "Memory Monitor" tool in Instruments, specifically the "free physical memory". If you use it, you will invariably see that your app will be killed when the "free physical memory" goes below a given limit (as per Instruments output), approaching zero left memory.
Now, whether that limit is reached because you allocated some memory (which is reported by the Allocations or Leaks tools) or because of other factors (a framework loaded into memory? something in the iOS kernel or that is not tracked by Instruments?) you cannot know. But for sure you can follow how the free physical memory in the device varies over time with the use of your app.
Hope this helps.
The first generation iPad has a total of 256 MB of memory. Of course some of that will be used by the operating system itself.
It's very conservative to assume that at least 64 MB would be available for your application. So 6 MB sounds pretty minimal to me. There must be something your application is doing that causes more memory to be allocated.
There definitely isn't some "I'm a game let me use more memory option". All applications are allowed to use as much memory as they can (though it's always a good practice to minimize memory usage). If memory runs low, the application is notified, and if memory is exhausted, the foreground application is terminated.
If your application is terminated, examine the crash log to see how much memory it was using. My guess is that you'll find you are using more than you think.
I would like to start testing my application on my iPhone. However, I have one concern.
What if my app has many memory leaks? If i ran such an app on the iPhone multiple times, wouldn't that result in a crash on my iPhone? i.e memory keeps getting allocated but never deallocated?
should i worry about accidentally creating unnecessary files on my iphone such that each time I run i somehow create a file that i dont delete, and it keeps taking up a few kb of space. I test the app many times and after a while i have no space left on my iphone. can such a thing happen? can testing on an actual device damage my phone???
Thanks!
What if my app has many memory leaks? If i ran such an app on the
iPhone multiple times, wouldn't that result in a crash on my iPhone?
i.e memory keeps getting allocated but never deallocated?
Memory leaks are only a problem as long as the memory is still allocated for your application. As soon as your application closes, or crashes, its memory is deallocated and returned to the OS. If you are running iOS 4 you can double click the home button and close the application to free up memory.
should i worry about accidentally creating unnecessary files on my
iphone such that each time I run i somehow create a file that i dont
delete, and it keeps taking up a few kb of space. I test the app many
times and after a while i have no space left on my iphone. can such a
thing happen?
Yea you should take care to create as few files as possible. However, because all files created by your app are contained in a little sandbox that is associated with your app they are deleted when your app is uninstalled. If you feel like your test app is taking up too much space simply uninstall it and then install it again using Xcode. Although I wouldn't worry too much about this as you would have to create A LOT of large test files before they started filling up your phone.
can testing on an actual device damage my phone???
I highly doubt its possible. That is one of the reasons that apple restricts which API's you can and can't use. If you jailbreak your phone and start doing things apple doesn't want you to do all bets are off.
I wouldn't worry too much about messing up your phone by developing on it. I've never heard of someone damaging their phone doing this and if for some reason you phone starts acting up just restore it in iTunes.
Here's a tip. Use Analyze tool in Xcode. Find leaks and remove them. And check the performance using Instruments. Fix your memory leaks using that as well. Once you do both diligently, there should be no reason to worry about leaks anymore. Your other concerns are a bit unnecessary. If you are using only the public cocoa touch APIs, your device should be perfectly safe :)
Also be sure to clean up any unused resources, etc. When you send it to the background, it only does so much. Make sure you release objects you own, and release the resources. Set autorelease when you can.
I'm developing a new application for the iphone.
I want to understand something - does the memory that my application uses shared with other applications?
What if an application causes memory leaks, does it effect the device performances after the user closes the application?
thanks.
At the moment, on iPhone you'll only be sharing memory with a certain few applications - MobilePhone, MobileMail, MobileMessaging, Safari, iPod and a few minor daemons.
If you're the active application and you need more memory than is free, then some of these applications (but not the most critical ones, like MobilePhone) will be terminated in order for you to continue. If you continue to use memory, you'll be terminated before the critical apps.
The amount of memory you can use depends on which device you're on and what else is going on on the device. Common advice is that as long as you stay under 30Mb, you'll be fine.
Once an application is terminated, the fact that it leaked memory (or not) is no longer an issue - the system will clean up and free it's entire address space.
Apps sometimes say that you should reboot your iPhone before running the app, clearing the RAM that critical daemons use for caching or whatever during normal use.
If you ever find yourself doing this, stop. If you ship an app with this requirement, you fail as a programmer and should be banned from programming. Why? Look up the amount of RAM a Playstation 2 has, then look at games like Gran Turismo and Grand Theft Auto.
I know this is a subjective question. As far as I know, there are somewhat about 25 MB available for the app, but it depends on what else is going on. Currently playing music, a current phone call or what ever might drop that amount of memory down a lot. I don't know.
Just tell us what you think, or what you have experienced. My app currently consumes about 15 MB at a time, mostly lots of images loaded into views for animation. I have encountered no problems with this. But is there some "border" that can be drawn, to make a simple-to-follow rule like "do not consume more than x MB of memory at any given time", where x stands for the max amount?
This value highly varies per device and per user. For example, I restart my phone every few days, freeing up lost memory claimed by Apple and 3rd party leaks. My mother, has not restarted her phone ever on her own. EVER. She has tons of trouble with apps, gives me a call and I tell her to restart the phone to increase the ram. She does and the apps dont crash anymore.
Moral of the story is: Never plan on having available memory. You should always keep your memory down to the bare minimum you need at any given time.
You can use Instruments and launch some apps and find out for yourself.
Recently I found this awesome tool to find what is the maximum memory capacity of any iOS device.
We can also find at which memory level we received the Low Memory warning.
here is the link: https://github.com/Split82/iOSMemoryBudgetTest
So, I finished writing my first iPhone App, and I have sent it out to a group of beta testers.
Everybody is happy, except for this one guy who noticed that after having run my app, another app is not starting up anymore.
Not knowing too much about memory management, I started looking at the Leaks graphs in Instruments, and noticed, that there are some leaks going on in my app.
Three questions:
Are leaks always bad? The biggest leak is 15k on a total of 5,1 MB allocated memory.
Will Apple refuse my app because of leaks?
Is not all the memory automatically freed up as soon as my app quits? Could it be that my leaking app is harmful to other apps?
Thanks
Sjakelien
Each app is supposed to run in its own "sandbox," and is by design not supposed to affect any other app installed on the phone. If your reviewer found a case where your app really did affect another app, that's definitely not your fault, although I'd love to know how that was accomplished ;).
My guess is, though, that your reviewer's other app broke completely independently of your app, and he/she is spuriously attributing the fault to your app. Ask him to try to reproduce the problem (uninstall both apps, install the other app, install your app).
what the other two answers say i agree with, however nobody has answered the first question ("Are leaks always bad") directly. i would say that yes they are. it's true that your program can possibly run with them for long periods of time without issue. and the OS should clean up the memory after termination (unless there's a bug in the OS, as said before). but eventually the leak will become an issue for someone at some point when they run long enough. also, even if it doesn't become an issue, it's probably a best practice to always fix memory leaks that you know about.
Any leak that persists beyond the exit of your application is an OS bug, and hence Apple's problem.
It would be unfair to reject your app on this basis.
I don't know if that's any comfort to you.
For the record, my app provokes a leak in mediaserverd.
The leak is a tiny bit smaller in the 3.0GM.
Another incorrect usage of an API crashed mediaserverd.
Also 3.0GM. Nasty. I'd have preferred an error code, however
this could theoretically be used to work around the leak.
It depends on what "memory" you are leaking. Are you using device storage space to cache something? In theory then it's possible that you've used up enough space the other app cannot start because it needs a certain amount of free space.
If we're talking physical memory, then no. When your app is dead it is dead. You could ask them to restart the device and see if they have the same issue.
Apple does approve apps that have memory leaks.
That said, we thoroughly check that none of "our code" leaks before submitting to Apple because users don't like having an app suddenly shut down on them. I say "our code" because there are memory leaks in the framework that are not your doing. Thus Apple approves apps with memory leaks.
Also, I am doubtful your app has adversely affected the user's other app. I have not seen that behavior.