how to clear entir cache from md2 model loading??

Started by Davi, November 15, 2011, 10:19:47 AM

Previous topic - Next topic

Davi

I have two activities.one is first page,it has a start button,when I clike this button,it will load my two md2 models at the same time ,when they load completely,it will skip two my game activity,in my game activity , this time,two models work normally, when I click key Back,it will go back to my first page,but at this time,if I click the start button again,my app will crashed,and there will be some error messages in logcat:


11-14 17:14:08.106: E/AndroidRuntime(19642): java.lang.OutOfMemoryError
11-14 17:14:08.106: E/AndroidRuntime(19642): at com.threed.jpct.Mesh.<init>(Mesh.java:143)
11-14 17:14:08.106: E/AndroidRuntime(19642): at com.threed.jpct.Mesh.cloneMesh(Mesh.java:332)
11-14 17:14:08.106: E/AndroidRuntime(19642): at com.threed.jpct.Loader.loadMD2(Loader.java:1328)
11-14 17:14:08.106: E/AndroidRuntime(19642): at com.threed.jpct.Loader.loadMD2(Loader.java:131)
11-14 17:14:08.106: E/AndroidRuntime(19642): at org.mxr.app.ModelLoading.loading(ModelLoading.java:75)
11-14 17:14:08.106: E/AndroidRuntime(19642): at org.mxr.app.ModelLoading.run(ModelLoading.java:65)
11-14 17:14:08.106: E/AndroidRuntime(19642): at java.lang.Thread.run(Thread.java:1102)
11-14 17:14:08.576: E/ActivityManager(1327): fail to set top app changed!


this is out of memorry,I just want to know how to clear entire cache after load md2 models before go back to first page?



EgonOlsen

Which cache? There is no cache or something. Personally, i would try to avoid reloading stuff at all. If you have to, make sure that all references to your models are cleared. Keep in mind that (as said before in another thread)



model=Loader.loadXXX(...);



isn't the same as


model=null;
model=Loader.loadXXX(...);

Davi

you 2 lines code mean the global variable Object3D model is not the same as local variable Object3D model.
I have done that in onDestory(),the code as following:

                @Override
protected void onDestroy() {
                model=null;
                fb=null;
                world=null;
                TextureManager.getInstance().removeTexture(texturesName);
                 Loader.clearCache();               
               }

above is right?

EgonOlsen

Quote from: Davi on November 15, 2011, 11:51:44 AM
you 2 lines code mean the global variable Object3D model is not the same as local variable Object3D model.
No, it has nothing to do with global against local. What it means is, that if model (for example) consumes 4mb, the first code will consume 8mb (old reference plus new reference) during load while the second one only uses 4mb. This isn't always clear, which is why i mention it from time to time when it comes to garbage collection related questions.
Quote from: Davi on November 15, 2011, 11:51:44 AM
I have done that in onDestory(),the code as following:

                @Override
protected void onDestroy() {
                model=null;
                fb=null;
                world=null;
                TextureManager.getInstance().removeTexture(texturesName);
                 Loader.clearCache();               
               }

above is right?
It's ok, but it would be better to do:


                @Override
protected void onDestroy() {
                TextureManager.getInstance().removeTexture(texturesName);
                model=null;
                fb.freeMemory();
                fb.dispose();
                fb=null;
                world.dispose();
                world=null;
                 Loader.clearCache();               
               }


Also make sure that this method will actually be called in your case. It's more likely that your Activity will get paused/stopped instead of being destroyed.

Davi

   
Quote
   @Override
    protected void onDestroy() {
                TextureManager.getInstance().removeTexture(texturesName);
                model=null;
                fb.freeMemory();
                fb.dispose();
                fb=null;
                world.dispose();
                world=null;   
                  Loader.clearCache();               
               }
thank you very much, it seems can't release all texture memory from my system,can you tell me how to clear all memory completely,
use as following code:

TextureManager.getInstance().removeTexture(texturesName);
TextureManager.getInstance().flush();
TextureManager.getInstance().removeAndUnload(textureName,frameBuffer)

right?

EgonOlsen

No. One call to removeAndUnload is sufficient. However, i still don't see the need to go through all this hassle. Just don't reload your data on resume of the Activity. Or at least leave the textures untouched. The TextureManager will keep track of the textures. That's the reason why it's there. There's no need to constantly remove and add a texture. If you absolutely have to, call unload to remove it from the GPU's memory, but leave it inside the TextureManager.