Author Topic: Memory usage growing  (Read 1859 times)

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Memory usage growing
« on: October 02, 2012, 06:59:28 pm »
Hi there,

In my wallpaper, the user can choose what mesh (Object3D loaded from .obj)  to use in the preferences, and what skybox textures to use for the background. I'm changing both things in onSurfaceChanged() and it seems the memory usage grows up every time the wallpaper is restarted (with an orientation change for example).

I'm calling the dispose() method of the Framebuffer, World, and SkyBox (don't know if other classes also have this method) classes when the surface is destroyed. Also, when I replaceTexture() I previously call unloadTexture(), but all theses things don't keep the memory usage from growing. I've even tried to call the flush() method of the TextureManager class when the surface is destroyed, but again, the memory usage keeps growing. Forcing the garbage collector to run doesn't free up any memory, so I think the memory is referenced somewhere, but not in my code, because I null all the references onSurfaceDestroyed()....

What else should I do ?

Thank you!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Memory usage growing
« Reply #1 on: October 02, 2012, 08:33:13 pm »
How are you loading your textures? If you load them as Bitmap or Drawable, Android will cache them. Does this increasing memory consumption reaches a limit or does it increase until you get an OutOfMemoryError?

You might want to profile you app (if it's actually possible to profile a wallpaper properly with the means that Eclipse and the Android plugin provide...) to see where the memory goes. Personally, i'm not aware of any existing memory leaks in the engine.

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Re: Memory usage growing
« Reply #2 on: October 03, 2012, 12:07:08 am »
How are you loading your textures? If you load them as Bitmap or Drawable, Android will cache them. Does this increasing memory consumption reaches a limit or does it increase until you get an OutOfMemoryError?

You might want to profile you app (if it's actually possible to profile a wallpaper properly with the means that Eclipse and the Android plugin provide...) to see where the memory goes. Personally, i'm not aware of any existing memory leaks in the engine.

Hi Egon,

I'm loading the textures like this:

Code: [Select]
TextureManager tm = TextureManager.getInstance();
String name = "light_" + mLightTex;
int id = res.getIdentifier(name, "drawable", mContext.getPackageName());
if (tm.containsTexture("light")) {
tm.unloadTexture(mFrameBuffer, tm.getTexture("light"));
tm.replaceTexture("light", new Texture(res.getDrawable(id), true));
} else {
tm.addTexture("light", new Texture(res.getDrawable(id), true));
}

And this is what I do to release resources onSurfaceDestroyed():

Code: [Select]
   
if (mFrameBuffer != null) {
    mFrameBuffer.dispose();
    mFrameBuffer = null;
}
if (mSkyBox != null) {
  mSkyBox.dispose();
  mSkyBox = null;
}
if (mWorld != null) {
    mWorld.dispose();
    mWorld = null;
}
TextureManager.getInstance().flush();

It seems the memory consumption keeps increasing, but does not make the program crash. The problem is that the text on the PreferenceActivity of my Wallpaper gets corrupted after a while.


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Memory usage growing
« Reply #3 on: October 03, 2012, 12:10:23 am »
Try to load the textures via an InputStream (for example: res.openRawResource(R.raw.texture)) instead and see if that helps.

What's the relation between memory usage and the text on the Activity?

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Re: Memory usage growing
« Reply #4 on: October 03, 2012, 01:33:37 am »
Try to load the textures via an InputStream (for example: res.openRawResource(R.raw.texture)) instead and see if that helps.

What's the relation between memory usage and the text on the Activity?

Unfotunately, it makes no difference if I load the textures via an Inputstream :(

Here (http://tinypic.com/r/28m2452/6) you have a short video (01:30) that shows the increase in memory usage. Everytime you see the data changes is because I've rotated the phone... Also, you can see that in the end, when the memory usage reaches 27MB it goes back down to 17/18MB...

I don't know what the relation is between the missing text in PreferenceActivity and the memory usage, maybe it has nothing to do, but the text corruption always happens after a few orientation changes, never at the beginning...

Also, I would like to know if it is really necessary to call dispose() on the World and SkyBox objects (I know you call it on the Framebuffer).

Thank you!
« Last Edit: October 03, 2012, 01:37:47 am by robert »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: Memory usage growing
« Reply #5 on: October 04, 2012, 08:50:19 am »
If the memory finally gets freed by the garbage collector, i don't see the actual issue. It's up to VM how to manage the memory and as long as it isn't a real leak (which it doesn't seem to bo or otherwise, the memory won't be freed again), i don't see a problem with that behaviour. You might want to try to call MemoryHelper.compact() in onSurfaceChanged...maybe that helps.
Regarding dispose()...you are meant to call it, if a reference goes out of scope. If it never does, there's no need to call it. Personally, i would simply keep my World and Skybox and don't create new ones all the time, but that's up to you.

Offline robert

  • byte
  • *
  • Posts: 40
    • View Profile
Re: Memory usage growing
« Reply #6 on: October 04, 2012, 12:31:35 pm »
If the memory finally gets freed by the garbage collector, i don't see the actual issue. It's up to VM how to manage the memory and as long as it isn't a real leak (which it doesn't seem to bo or otherwise, the memory won't be freed again), i don't see a problem with that behaviour. You might want to try to call MemoryHelper.compact() in onSurfaceChanged...maybe that helps.
Regarding dispose()...you are meant to call it, if a reference goes out of scope. If it never does, there's no need to call it. Personally, i would simply keep my World and Skybox and don't create new ones all the time, but that's up to you.

Hi Egon,

After some more testing, it seems the PreferenceActivity text corruption does not only affect my wallpaper. I've tried other wallpapers and if you change the orientation a few times, some text in the PreferenceActivity will be missing.

Regarding the memory usage, it seems you are right, there are no leaks. The issue with the preferences confused me...

Thank you for your support.