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.
Related
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)
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
What is the amount of memory an app can take before getting kicked by iOS?
Does the amount of memory depends on the device version?
I have developed an app which is using 30+ mb and its getting kicked on iPhone 2g. Can it work on iPhone 4 or 3GS?
My experience with the iPhone 3G is that you should try to stay as small as humanly possible--build your data model with ditchability in mind, because you'll need to ditch. 20mb is bumping against the limit. 25 MIGHT be okay if the phone has been rebooted recently. You'll probably never get 30mb.
By contrast... I managed to prompt a memory warning on my iPhone 4 once, but it was due to an infinite loop bug that downloaded the same image file an infinite number of times. In other words, it took something REALLY drastic to crush the 4. Not that you can ignore memory management completely (a leak is still a leak), but for sure you've got some breathing room.
The 3Gs is somewhere between the two. I don't have one to test on, but I'd expect its performace is more 4-like than 3G-like, because while the on-board memory doubled from the 3G, the OS is still taking up the same space, meaning all of the new memory is yours to play with.
All your application's resources on an iPhone 2 should probably stay at less than 20 MB. You can go a little over, but that's it, otherwise the memory warnings will occur. There's only 128 MB of total physical ram for everything - that's the OS as well as your own app.
I keep getting an out of memory failure when I try to do the distribution build of my app. The app only gets up to a max of 12 megs of real memory when testing it in Instruments. Why is this happening? My app folder is only 18 megs without the build folder in it. Turns to 80 megs with the build folder. But this doesn't seem like a lot compared to other big apps I've seen in the app store. Any ideas?
The size of the application on disk and the amount of memory required by the application when it is running are two different values. The failure you are seeing is relating to the second measurement, not the first. 12MB of memory consumed sounds like quite a bit. While this value is small for the Simulator being run on a machine with gigabytes of available memory (and an on-disk VM system), on the device where 256MB total memory is available it is a much bigger deal. In my experience I've found about 8MB to be an upper limit in memory consumption before things start going south (YMMV).
Is your application responding correctly to low memory warnings? Your app will be notified by the OS when things start to get hairy- not responding to the warnings appropriately can result in your application being preemptively terminated.
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.