I've been trying to keep my iOS Apps memory footprint nice and low, however, I'm not really sure what a good threshold is for either real or virtual memory is iOS.
At the moment a very simple app is using 12MB real memory and 70MB virtual memory but this is going to increase as my app becomes more complex.
My question is, what are the real and virtual memory levels that I should try to keep below to avoid getting memory warnings?
I realise that this is not an exact science as there are other factors like the memory footprint of other apps on the system but a rough answer would be welcomed.
This is indeed not at all an exact science. In my experience it's at least as important to release enough memory immediately when receiving an OS request to do so as it is to keep memory low.
That being said, as long as I keep my apps under ~80 MB real memory on an iPad I typically don't get any warnings (for refernce, iPad 1 has 256MB). (keep in mind, this is the full memory used, including screen buffers etc. allocated by the OS for your app, not just what you allocated yourself)
Related
how do i increase the available memory (more concrete: the part of the RAM) that can be used by my app?
i am not referring to the space available on the harddisk of an iphone, but to the RAM.
does anyone know if there's a way to use the whole free part of the RAM for my app only?
i have to clear this up a bit, sorry if it was too vague:
i had to think of the java heap size, which can be increased by adding a parameter to the startup command of the java programming.
as i don't know, but at the moment suppose on the iphone something similar happens (every app has just xxMB RAM for execution) this value might be adjustable, so that i can use the whole remaining RAM for my app.
which pretty much is what i need for this special app (non-app-store-publication; file-sizes between 50MB and several 100MB)
How do i increase the available memory?
Short answer: you don't.
Memory is managed by the kernel.
Your application process can't control this.
You can't explicitly control this - this is managed by iOS.
You can't do that. Try reducing your memory usage instead of looking for ways to remove well needed limits.
Store your data instead and read from it when needed.
I don't know for sure if you can configure RAM allocated for your app. I think that is taken care of by the iOS kernel
It's unlikely (read: not going to work) that you will be able to allocate anything more than a few Mb in your application at once.
Not planning on publishing your app to the App Store won't change this. Apple don't officially acknowledge the amount of memory in iOS devices. But its known that devices have between 128Mb and 512Mb of physical RAM.
With the kernel, essential applications (Phone app, etc), background processes, etc, you won't have anything like that available to your application. Careful analysis in instruments would suggest that you'll generally start getting memory warnings when you've allocated around 22Mb of RAM in your application.
A change made in iOS 5 makes the watchdog process much more aggressive with killing applications after you get a memory warning. If you get a memory warning on iOS5 you have to reduce your memory usage or you will get jettisoned by the OS.
If you want to proceed, you will have to figure out how to reduce the amount of memory your datasets require. Its unlikely that all of the 100Mb file needs to be in memory at once. iOS devices have "relatively" fast CPU's and storage, you'll have to architect your application to read and write to storage in chunks and work on smaller subsets of your data.
Some related Stackoverflow questions and links:
Monitor memory usage in an iphone app?
How much memory can an iPhone app use?
10 iPhone Memory Management Tips
I've recently been developing an app that processes a large amount of data very frequently (~15 times a minute). To do so, I allocated a large chunk of memory, then freed it for each batch of data.
Here's a screen of my Memory Allocations from Instruments:
The Memory usage oscillates from about 3MB to about 30MB pretty quickly. I was just wondering, is this "healthy," per se for the iPhone.
Is it risky to allocate and free so much memory so quickly? Is it unsustainable, or just bad practice?
Thanks!
It is neither risky nor necessarily bad practice. Allocating and freeing memory takes time, so doing it very frequently vs. doing it once and re-using the allocated memory is a trade-off between memory usage efficiency (using the lowest amount of memory at every single moment) and performance.
If the performance of your app doesn't suffer at the moment, you have probably made the correct choice regarding this tradeoff for your app.
Generally speaking, using 30 MB of memory is quite a large amount for older devices (iPhone 3G and older). You cannot be sure that your app has that much memory available so be prepared to received memory warnings. If your app cannot reduce its memory usage when it receives a memory warning, the OS might kill it.
My primary worry in these situations would be fragmentation. If the chunks are all the same size though, you should be fine (and looking at your graph, the peaks appear to be completely level, so I think that's the case).
You will be paying allocation costs, but as Ole says, if your app is performing well enough already, there's not much point in trying to optimize that.
It depends, if the user has an iPhone 4 or iPhone 3GS it should be do-able but on the iPhone 3G it will result in a memory warning very quickly.
iPhone 4 has 256mb of RAM for the apps ( 512 mb in total )
iPhone 3GS has 128mb for the apps, and 256 in total
the iphone 3g only has 128mb and 64mb for the apps.. usually having around 40mb free when no apps are running.
As apple says you should only allocate the memory you really need, and try to not use autorelease too much, because autorelease gives us an object being allocated while we
don't really need it anymore
If the performance isn't too bad, I would try using less memory and allocating more when you really need it.
My app uses about 50 MB of memory at startup when I look at dirty memory using the VM Tracker instrument. It's a pretty simple app, and from what I recall at WWDC this year, this seems like way too much memory use for an app with only one simple UIWebview at startup.
I think I recall someone from Apple telling me to keep my memory use below 20MB as a guideline.
Before I go off and try to track this down - what's a good target to keep performance smooth and mem warnings reasonable?
Virtual Memory != RSIZE. Don't get confused. Virtual Memory includes all sorts of shared memory, including mapped files and frameworks. What you really care about keeping low is your resident memory (RSIZE).
How much real memory should my iphone app be using? What's going too high?
Keeping an eye on -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application is definitely important, but if this is game, chances are the assets (notably textures presently on screen) can't simply be deallocated when that warning is received.
If you're a bit on the high side (20 MB +) I would recommend doing a bit of testing. Using Instruments and the Object Allocation tool (Run > Run with Performance Tool > Object Allocations) you can monitor how large your memory footprint gets. Then, try running Safari and fill the pages, then a few games and whatever else you can to get the memory higher, and see how your app performs.
In my testing for a recent release, 24 MB seemed to be pretty safe, and is a number I've heard elsewhere. Once you get above 30 or so MB, chances are your users will start having rare crashes (which happens to be the case for us, verified by crash reports). The higher you go, the more crashes users will see. There's no specific limit though, for the sake of testing I've pushed my app on an iPhone 3G up to 70 MB before, it just isn't likely to work for most, nor for long.
Requesting on the iTunes page that users restart their devices can help, though there's no guarantees it'll be effective.
Also, this is all assuming devices prior to the 3GS / 3rd Gen iPod Touch. If the app merely runs on an older device, it should have no problem on the newer ones (which have twice as much ram, 256 MB).
I think available memory may depend on several factors such as device model, how long it has not been rebooted etc.
You should not rely on some fixed values but instead try to use as little memory as possible and implement -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application in your application delegate and/or -didReceiveMemoryWarning in your view controllers to handle low memory warnings there and free unused memory.
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