Out of memory on resource loading for Android game
<p>I'm writing a game that so far has to work with about ~200 PNGs totaling ~14 MB, all with sizes ranging from 250x150 to 600x400 (these are xhdpi drawables, right now I'm letting android do the resizing for lower dpi). </p>

<p>The problem is that if I load them all when the game starts, I get an out of memory error after about 20 seconds, both on the emulator (with 1 GB RAM) and on a Galaxy S device (512 MB RAM). </p>

<p>I've seen games with much bigger resources, some even in the hundreds of MB. How do these handle loading their resources fast without exceeding the memory constraints? 14 MB shouldn't be that much.</p>

<p>Right now I'm using <code>BitmapFactory.decodeResource()</code> to load every bitmap inside a few for loops before the game starts. I also tried using a <code>BitmapFactory.Options</code> with <code>inSampleSize</code> set to 4, which fixes the out of memory problem, but not the slow loading, which still takes about 20 seconds. I would also rather not have to do this, because if I make the images 4 times smaller there is little point in accounting for hdpi and xhdpi screens at all - quality will be a lot worse on these. I also tried using <code>getDrawable()</code>, but it made no difference.</p>

<p>I also considered loading each resource as it is needed (after the game has started), but:</p>

<ol>
<li>Wouldn't this be slow? - right now the system allocates more memory pretty much between each load, which takes 100-300 ms. This would seriously slow down my frame rate.</li>
<li>If I cache each image so it's only loaded once, eventually I will need all of them in the same level, so this shouldn't fix the out of memory error.</li>
</ol>

<p>I know a bitmap takes more space in memory than the PNG on the disk - I get the error when allocating about 40 MB. Isn't there a way to just load the 14 MB in memory and build bitmaps from them as they are needed, or something that gives up a little speed (it's a pretty basic 2d game so I don't really need a lot of processing power or max fps) in exchange for a lot more free memory?</p>

<p>Also, I have a lot of small animations with about 10-16 frames. Each one is a separate file. Would there be any benefit in merging them in one single file and loading that? I can't see it helping with memory usage, but could it help with loading times?</p>

<p><strong>Update</strong>: I changed my code to load each frame, display it, and then recycle() it. This leads to what I can tell is &lt; 5 FPS, so I don't think I can do this</p>
