Author Topic: how to clear entir cache from md2 model loading??  (Read 3326 times)

Offline Davi

  • byte
  • *
  • Posts: 33
    • View Profile
how to clear entir cache from md2 model loading??
« on: November 15, 2011, 10:19:47 am »
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:
Code: [Select]

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?



Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: how to clear entir cache from md2 model loading??
« Reply #1 on: November 15, 2011, 10:30:44 am »
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)

Code: [Select]

model=Loader.loadXXX(...);


isn't the same as

Code: [Select]
model=null;
model=Loader.loadXXX(...);

Offline Davi

  • byte
  • *
  • Posts: 33
    • View Profile
Re: how to clear entir cache from md2 model loading??
« Reply #2 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.
I have done that in onDestory(),the code as following:
Code: [Select]
                @Override
protected void onDestroy() {
                model=null;
                fb=null;
                world=null;
                TextureManager.getInstance().removeTexture(texturesName);
                 Loader.clearCache();               
               }
above is right?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: how to clear entir cache from md2 model loading??
« Reply #3 on: November 15, 2011, 02:25:25 pm »
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.
I have done that in onDestory(),the code as following:
Code: [Select]
                @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:

Code: [Select]
                @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.

Offline Davi

  • byte
  • *
  • Posts: 33
    • View Profile
Re: how to clear entir cache from md2 model loading??
« Reply #4 on: November 17, 2011, 09:17:20 am »
   
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:
Code: [Select]
TextureManager.getInstance().removeTexture(texturesName);
 TextureManager.getInstance().flush();
TextureManager.getInstance().removeAndUnload(textureName,frameBuffer)
right?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: how to clear entir cache from md2 model loading??
« Reply #5 on: November 17, 2011, 09:42:10 am »
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.