Out of Memory Exception while loading Textures

Started by Yerst, July 13, 2013, 09:20:36 PM

Previous topic - Next topic

Yerst

Hey!
I'm trying to set up a SkyBox, but when i try to load the textures (tm.addTexture("left",new Texture(this.context.getResources().getDrawable(R.drawable.left)));) the program crashes with an out of memory exception after 4 textures (of 6).
One texture has a size between 150 and 300 KB, all textures have 1.32 MB.
I don't know why i get a out of memory at already 1.32 MB, but does anybody know a workaround?

Irony

Compressed image file size does not mean a thing. After unpacking, your texture needs x*y*4 bytes of memory.

Yerst


EgonOlsen

No, they are NOT 1.x mb in size. That's the size of the compressed jpg files. It has nothing to do with the size that a texture uses in memory. I've posted about this several times already, so i'm just linking to the wiki. It contains the basic formular to calculate memory usage of a texture as well as tips on how to reduce it: http://www.jpct.net/wiki/index.php/Reducing_memory_usage

EgonOlsen

Btw: How large are these textures (in pixels)?

Yerst

1024*1024*24, but isn't there a way of getting more memory?
I think i can't write a whole game with such hard memory limits.
I will need much more models and textures...

EgonOlsen

#6
No, not really. You can ask for more memory in your manifest but you can't be sure that every device supports this. Memory depends on the version of Android and you should aim for the  amount that your target version provides. You skybox textures alone will consume around 64mb of memory (6*4mb*2 + mipmaps).
Remember that this still is a mobile device, not a desktop PC. You CAN create a complete game within these limits, just not with skybox textures of that size...

Yerst

So, it works when i use 512x512 textures, but my problem now is (and i don't know if i should open another topic for that) that i need to call the BitmapHelper
tm.addTexture("left",new Texture(BitmapHelper.rescale(BitmapHelper.convert(this.context.getResources().getDrawable(R.raw.left)), 512, 512)));

and rescale my texture to 512x512. The crazy thing is that it is already 512x512.
If i don't rescale it i get a "Unsupported Texture Width: 768" exception.
So i scaled down my texture (in paint) to 341x341 without resizing it with the BitmapHelper  and it worked.
How can i still use my 512x512 textures?

EgonOlsen

Don't store them into the drawable folder and/or don't read them as a drawable, because Android will rescale those based on your device's density. If you store them in raw, load them via


new Texture(res.openRawResource(R.raw.blahblah));


or store them in the assets-folder (which is what i prefer).