Author Topic: Not able to clear the data  (Read 2564 times)

Offline JKumar

  • byte
  • *
  • Posts: 33
    • View Profile
Not able to clear the data
« on: October 12, 2010, 06:04:39 pm »
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?


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Not able to clear the data
« Reply #1 on: October 12, 2010, 08:11:08 pm »
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.

Offline JKumar

  • byte
  • *
  • Posts: 33
    • View Profile
Re: Not able to clear the data
« Reply #2 on: October 13, 2010, 10:25:35 am »
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.


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Not able to clear the data
« Reply #3 on: October 13, 2010, 08:21:52 pm »
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.

Offline Ulrick

  • byte
  • *
  • Posts: 18
    • View Profile
Re: Not able to clear the data
« Reply #4 on: October 15, 2010, 10:04:44 am »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Not able to clear the data
« Reply #5 on: October 15, 2010, 10:41:28 am »
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

Code: [Select]
obj=Loader...
...
obj=Loader...

isn't the same as

Code: [Select]
obj=Loader...
...
obj=null;
obj=Loader...

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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Not able to clear the data
« Reply #6 on: October 15, 2010, 12:47:40 pm »
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:

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

Offline JKumar

  • byte
  • *
  • Posts: 33
    • View Profile
Re: Not able to clear the data
« Reply #7 on: October 17, 2010, 12:33:06 pm »
Hi,

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

Thanx a lot :)

Offline JKumar

  • byte
  • *
  • Posts: 33
    • View Profile
Re: Not able to clear the data
« Reply #8 on: October 17, 2010, 02:36:52 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: Not able to clear the data
« Reply #9 on: October 17, 2010, 09:39:17 pm »
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.