FrameBuffer.freememory sometimes useless?

Started by Uncle Ray, April 27, 2015, 11:04:34 AM

Previous topic - Next topic

Uncle Ray

Solved by ego
Answer:GLSurfaceView.setPreserveEGLContextOnPause(true);

_______________________________________________________
without touch phone's home button,freememory method it was good.
it free memory immediately,very nice.
but if i touch phone's home button and return to my app again.
the freememory method was never work,and the memory was never free.


may be my bug was in onSurfaceChanged?

here is my code:

public void onSurfaceChanged(GL10 gl, int width, int height)
{       
  fb=null;
  fb=new frameeBuffer(width,height);
  Config.unloadImmediately=true;                   
  texturemanage.removeAndUnload("a", fb);      
  texturemanage.removeAndUnload("c", fb);
   ................................................
  fb.freememory;
}



EgonOlsen

freeMemory() basically unloads all known textures (to the TextureManager) from native memory. This should actually happen if the context get's destroyed anyway, so it might not be very useful. You can monitor what it does if you set the Logger to debug mode. That should give you some log outputs like "Unloaded texture: <int>".

Anyway, what's the point of doing this in onPause and how do you judge that it doesn't do anything?

Uncle Ray

because,if i without  touch home button,like "Unloaded texture: <int>",will display.

but,if i touch home button and return my app,like "Unloaded texture: <int>".was disappear.

yes,your are right,may the old context is destroyed,but their memory never freed,after i return to app,the freememoy just freeed the new context's memory,the old context's memory will never free,although the old context was destroyed.


i have soled,in this way,meybe there is better solution?can you tell me?
public void onSurfaceChanged(GL10 gl, int width, int height)
{       
  fb.freememory;//unload old context's  framebuffer memory
   
  fb=null;
  fb=new frameeBuffer(width,height);
  ini_tex(fb);//initialize new context's framebuffer
  Config.unloadImmediately=true;                   
  texturemanage.removeAndUnload("a", fb);       
  texturemanage.removeAndUnload("c", fb);
   ................................................
  fb.freememory;
}



Uncle Ray

in onPause,i did nothing just super method

protected void onPause()
{
      
      super.onPause();
      mGLView.onPause();//GLSurfaceView mGLView
      
}

EgonOlsen

Putting it at that position actually makes no sense, because in onSurfaceChanged, the old GL context is already invalid. Even if it actually prints out some messages, it doesn't really free stuff because it's working in a different context, where these textures don't exist. In addition, I don't quite understand why people are so keen on un- and reloading everything on pause or stop.
Here's what I do and it works fine for me: Load all textures once and leave them be. Call setPreserveEGLContextOnPause(true); on your GLSurfaceView and store the last gl instance. In onSurfaceChanged, check if the new instance is different from the old and only if it is, create a new FrameBuffer. That's all i'm doing. I don't see the need to constantly load and unload stuff. If the gl context gets destroyed (which can happen with setPreserveEGLContextOnPause(true); as well in some situations), the native memory used for the textures will be freed by the driver anyway. It has to be this way. Everything else would kill the OS after some time.

Uncle Ray

yes,yes,the very key is the GLSurfaceView.setPreserveEGLContextOnPause(true);it solved my problem perfectly.
Giant appreciation to you ego,Nothing i can see,without thx.