Author Topic: Out of memory, performance issues  (Read 3209 times)

Offline RhoX

  • byte
  • *
  • Posts: 29
    • View Profile
Out of memory, performance issues
« on: January 03, 2012, 09:44:54 pm »
Hello everyone,

I've been developing a new game for Android with the jPCT-AE. Recently I've faced a new problem, an error the occurs randomly. Here is the log:

Code: [Select]
01-03 15:16:01.262: ERROR/dalvikvm(2038): Out of memory: Heap Size=20231KB, Allocated=16823KB, Bitmap Size=4274KB
01-03 15:16:01.282: WARN/dalvikvm(2038): threadid=15: thread exiting with uncaught exception (group=0x4001b390)
01-03 15:16:01.282: ERROR/AndroidRuntime(2038): Uncaught handler: thread GLThread 10 exiting due to uncaught exception
01-03 15:16:01.282: ERROR/AndroidRuntime(2038): java.lang.OutOfMemoryError
01-03 15:16:01.282: ERROR/AndroidRuntime(2038):     at com.threed.jpct.Texture.loadTexture(Texture.java:701)
01-03 15:16:01.282: ERROR/AndroidRuntime(2038):     at com.threed.jpct.Texture.loadTexture(Texture.java:662)
01-03 15:16:01.282: ERROR/AndroidRuntime(2038):     at com.threed.jpct.Texture.<init>(Texture.java:130)
01-03 15:16:01.282: ERROR/AndroidRuntime(2038):     at android.jpct.screening.ScreenRenderer.onSurfaceChanged(ScreenRenderer.java:139)
01-03 15:16:01.282: ERROR/AndroidRuntime(2038):     at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1122)
01-03 15:16:01.282: ERROR/AndroidRuntime(2038):     at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:977)

This Out of Memory error occurs often, but not always. Actually, sometimes I run my code (in a device) and everything works just fine, other times (without making any changes) I get this error.
I've looked after some performance issues, memory usage, this kind of stuff.. but I still get this error.
Any suggestions?

Thanks in advance

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Out of memory, performance issues
« Reply #1 on: January 03, 2012, 10:33:26 pm »
Android sometimes behaves a little erratic when working with Bitmaps with an almost filled vm memory. There's no real solution to this other than making it use less memory. How large is that texture? Have you followed these suggestions: http://www.jpct.net/wiki/index.php/Reducing_memory_usage?

Offline yindroid

  • byte
  • *
  • Posts: 21
    • View Profile
Re: Out of memory, performance issues
« Reply #2 on: January 04, 2012, 01:35:15 am »
try to use DDMS tool and watch for heap size!

Offline RhoX

  • byte
  • *
  • Posts: 29
    • View Profile
Re: Out of memory, performance issues
« Reply #3 on: January 04, 2012, 05:39:01 pm »
I'm not used to the DDMS tool, but I will try it.

About reducing memory usage, I have followed some of the tips. In fact, I can see the performance difference but the problem still occurs. My textures are all about 512 x 512 (with 285kb) at most - for the skybox - the others are 128x128, 128x64 (with 1.8kb, 1.17kb)... Maybe are the skybox textures too big?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Out of memory, performance issues
« Reply #4 on: January 04, 2012, 08:45:30 pm »
As written in the wiki: The file size doesn't matter at all. What matters is the size in memory. 512*512=262144 pixels * 4 bytes = 1mb. One 1mb is needed to store the texture in the gpu's ram (when using 32bit color depth), 1mb for keeping a copy in memory (the upcoming version of jPCT-AE will be able to swap this memory to disk instead...) => 2mb. Plus mipmap, i.e. +256*256*4 + 128*128*4+64*64*4...~ 2.5mb per 512*512 texture. A skybox has 6 textures=> 6*2.5mb = 15mb for the skybox texture alones. As written in the wiki, you can reduce this by defaulting to 16bit textures and compress the in-memory copy.

May i ask which version of Android you are using?

Offline yindroid

  • byte
  • *
  • Posts: 21
    • View Profile
Re: Out of memory, performance issues
« Reply #5 on: January 05, 2012, 03:37:49 pm »
very interesting calculations! About cempressions, is it possible on android devices? (android 2.1)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Out of memory, performance issues
« Reply #6 on: January 05, 2012, 08:20:43 pm »
Yes, it's possible.

Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Out of memory, performance issues
« Reply #7 on: January 09, 2012, 08:43:43 am »
You have over 16MB already used up so you are running out of RAM when uploading the textures.  The textures themselves don't use much Application RAM after being loaded since they are sent to the GPU VRAM, so you basically need to cut down on the other resources. i.e. use low poly objects or clone the objects if loading the same object multiple times, or load the textures first before anything else.
« Last Edit: January 09, 2012, 08:47:10 am by K24A3 »

Offline RhoX

  • byte
  • *
  • Posts: 29
    • View Profile
Re: Out of memory, performance issues
« Reply #8 on: January 09, 2012, 05:36:51 pm »
Hello guys, thanks for the help.

Im using Android 2.1 version.
I kinda solved the problem with this links:

http://www.google.com.br/search?ix=icb&sourceid=chrome&ie=UTF-8&q=java.lang.OutOfMemoryError%3A+bitmap+size+exceeds+VM+budget
http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android
http://developer.android.com/resources/articles/avoiding-memory-leaks.html

There are some information about memory leaks, context, static and final variables. My big problem (I think) was the changes between landscape and portrait screens... those was "duplicating" the memory space used by my resources - those links explain better.

Thanks again for all that help! Ill keep it in mind through my development!

ps: K24A3,

I dont know it the problem is related somehow with my objects (for example), cause Im using just a skybox, a plane and a simple human model. But the idea of loading the textures first before anything else is interesting.