Is Spiky Memory "Healthy" for App?

286 Views Asked by At

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

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!

3

There are 3 best solutions below

2
On BEST ANSWER

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.

0
On

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.

3
On

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.