Author Topic: Out of Memory Exception while loading Textures  (Read 3130 times)

Offline Yerst

  • byte
  • *
  • Posts: 38
    • View Profile
Out of Memory Exception while loading Textures
« on: July 13, 2013, 09:20:36 pm »
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?

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Re: Out of Memory Exception while loading Textures
« Reply #1 on: July 13, 2013, 11:22:04 pm »
Compressed image file size does not mean a thing. After unpacking, your texture needs x*y*4 bytes of memory.

Offline Yerst

  • byte
  • *
  • Posts: 38
    • View Profile
Re: Out of Memory Exception while loading Textures
« Reply #2 on: July 13, 2013, 11:23:30 pm »
So, how can i still load the images?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Out of Memory Exception while loading Textures
« Reply #3 on: July 13, 2013, 11:25:04 pm »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Out of Memory Exception while loading Textures
« Reply #4 on: July 13, 2013, 11:26:57 pm »
Btw: How large are these textures (in pixels)?

Offline Yerst

  • byte
  • *
  • Posts: 38
    • View Profile
Re: Out of Memory Exception while loading Textures
« Reply #5 on: July 13, 2013, 11:32:31 pm »
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...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Out of Memory Exception while loading Textures
« Reply #6 on: July 13, 2013, 11:55:06 pm »
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...
« Last Edit: July 13, 2013, 11:56:44 pm by EgonOlsen »

Offline Yerst

  • byte
  • *
  • Posts: 38
    • View Profile
Re: Out of Memory Exception while loading Textures
« Reply #7 on: July 14, 2013, 11:05:26 pm »
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
Code: [Select]
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Out of Memory Exception while loading Textures
« Reply #8 on: July 14, 2013, 11:39:20 pm »
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

Code: [Select]
new Texture(res.openRawResource(R.raw.blahblah));

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

Offline Yerst

  • byte
  • *
  • Posts: 38
    • View Profile
Re: Out of Memory Exception while loading Textures
« Reply #9 on: July 15, 2013, 05:11:44 pm »
Thx, that worked great!