Not able to clear the data

Started by JKumar, October 12, 2010, 06:04:39 PM

Previous topic - Next topic

JKumar

HI,

         I am trying to load the followings

1. A serialized md2 model.
2. A texture
3. A primittives.

It runs fine for the first time but when i restart the app, it gives me OutOfMemory exception.

i used

mCharacter.clearObject();


but the same issue is coming.


Can anyone help out in getting  a way as How to release the resources onDestroy?


EgonOlsen

clearObject() isn't meant to be used for this. Actually, it's not meant to be used at all except for some very special cases. IF you want to reload the model, you have to remove it from the world, null all references to it in your own code and then load it again. But you don't have to, if this is about Activity handling. Just re-use the orginal instance in the new Activity.

JKumar

Hi,


   There are 2 cases i encountered using your NinjaDemoActivity class

1.When declaring varibales as attribute of the activity.

    I added following code taking in the try block in onCreate method of your NinjaDemoActivity class

mCharacter  =    null;
mArena     =   null;
mAnimation   =    null;
         
mCharacter   =  Loader.loadSerializedObject(getResources().openRawResource(R.raw.model_new5));
mArena        = Primitives.getPlane(4, 15);
mAnimation   = mCharacter.getAnimationSequence();
      
I have not added any of the object to the world. Its just the above lines only.
This run correctly for first time but thrwos OutOfMemeory when i restarts the app.

And

2.When declaring varibales as local(inside a block or method).

Object3D mCharacter     =    null;
Object3D mArena     =   null;
Animation mAnimation    =    null;
         
mCharacter   =  Loader.loadSerializedObject(getResources().openRawResource(R.raw.model_new5));
mArena        = Primitives.getPlane(4, 15);
mAnimation   = mCharacter.getAnimationSequence();

In this case everything runs fine at evertime i restart the app.



As i need the attributes for the activity to be used a other places also. I feel the data is not getting cleared in the engine.


EgonOlsen

And you are not adding them to a world in some other place? What's the point in loading them then? Seems to me like somebody is still keeping a reference to the instance. Either you in your own code or jPCT, because you are adding them to a world. Loading alone doesn't need anymore memory than the instance itself needs. Once nulled, it can be collected by the garbage collector. Another option might be, that the gc just sucks on Android. Try to call the compact() of the MemoryHelper in jPCT-AE's util package to see, if this frees some memory. If not, check for references in your code.

Ulrick

Hi,

I actually have a similar problem.

In my case, I load everything and it work perfectly at every run, but now I would like to change loaded object while the application run, but I have problems when I try to free memory.

In my case I don't want to clear the world object, but just "unload" some 3d objects and textures and load some new objects.

As far as I unload Textures, it works fine with the memoryhelper in cleaning memory, but it seems that 3d objects are not cleaned (actually the test I did was to load a 3d object, than unload it and load a new one, but I get out of memory exception (in the test, it is not the only object I have in the world, actually used memory is already at the limit when loaded the first 3d object and I need to unload it to loa a new one, because just loading the new one give the out of memory exception).

I tried already to invert the loading in order to be sure that was not a problem with the second object, and it works.

To make things clearer, my case is that for example I have 10 objects loaded with memory usage at the limit. One object more fire the out of memory, so I need to unload one object in order to load a new one.

How can I unload an object? Am I doing something wrong?

Thanks,

Massimo

EgonOlsen

How are you doing the "unload"? By using World.removeObject(...)? That should actually get rid of all internal references to that object!? Also keep in mind that


obj=Loader...
...
obj=Loader...


isn't the same as


obj=Loader...
...
obj=null;
obj=Loader...


Because the former one will deference the old object AFTER loading the new one, not before.

EgonOlsen

I've just noticed that there is some little memory leak in the Android version that can cause a reference to an old world being stored for another frame before it goes away. This might be a problem in some cases (it shouldn't, if you do a removeAll()/dispos()-call on World before nulling the reference). Until this is fixed, you might give this hack a try:


    Class<?> clazz = Class.forName("com.threed.jpct.ObjectArrays");

    Field[] fields = clazz.getDeclaredFields();
    for (Field f : fields)
    {
      f.setAccessible(true);
      if (f.getType().equals(Object[].class))
      {
        Object[] objs = (Object[]) f.get(clazz);
        for (int i = 0; i < objs.length; i++)
        {
          objs[i] = null;
        }
      }
    }


Just insert that before a world instance goes out of scope and see if it helps.

JKumar

Hi,

        This helped a lot and i am able to run any number of time without any exception.

Thanx a lot :)

JKumar

Hi,
     
          Though i am able to run the GLActivity(the main class using renderer and other jpct utilities ) without any exception but my others screens getting affected when coming out of GLActivity.

  All the other screens are running fine but when i enter in the GLActivity and come out of it. Other activities /screens starts showing OutOfMemory and its very random(not specific to any acitvity/screen ).

I don't know what causing it but its coming only when going inside the GLActivity(the main class using renderer and other jpct utilities ).


Any idea as whether gc is not able to clean the unwanted memory or any other issue.

EgonOlsen

Sounds like as if the gl activity simply consumes so much memory for what it does, that the other activity have not much memory left to work with...try to reduce the memory usage of the gl activity and see if that helps.

BTW: I've uploaded a new AE-jar, that should make the hack posted above obsolete. If not, please let me know how exactly you are trying to get rid of the old objects if not by removing them from the world.