Proper way to remove/delete Physics Objects to avoid memory leaks

Hi Everyone, knew to jMonkey + jMonkey Physics2.  I'm currently using the CVS versions of both, and am running into an issue with a memory leak related to removing objects from my world.



I have a series of complicated 3D worlds i need to load/unload as the player switches levels.  As such, i'm trying to remove the world from the physics space, load and add the new world.  This works, as far as the user can see, but its pretty obvious from the memory usage, that there is a memory leak.  I created a small sample bit of code to reproduce the problem, it is pasted below:



  public void clearWorld()

    {

        rootNode.detachAllChildren();

        m_physicsSpace.delete();

        m_worldStaticNode.delete();

        m_worldStaticNode = null;

        m_visualNode = null;



        rootNode.updateRenderState();





        System.gc();

    }

   

       

    public void createWorld()

    {

        Random rand = new Random();

        Vector3f newStart = new Vector3f((rand.nextFloat() * 10 - 5),

                                        (rand.nextFloat() * 10 - 5),

                                        (rand.nextFloat() * 10 - 5));

       

        m_worldStaticNode = m_physicsSpace.createDynamicNode();

        m_worldStaticNode.setName("worldStaticNode");

       

        m_visualNode = new Node("torusBaseNode");

        m_visualNode.attachChild(new Torus( "torus", 30, 20, 1, 4 ));

        m_worldStaticNode.attachChild(m_visualNode);

        m_worldStaticNode.generatePhysicsGeometry(m_visualNode,m_worldStaticNode,true);

        //m_worldStaticNode.generatePhysicsGeometry(m_visualNode,m_worldStaticNode,false);

        //m_worldStaticNode.createBox("testBox");

       

        //m_worldStaticNode.setLocalTranslation( newStart );

       

        //rootNode.attachChild(m_worldStaticNode);

       

        rootNode.updateRenderState();



    }







In my main code, i create my physics space, and then loop thousands of times around:



createWorld();

clearWorld();



So as to see if i'm getting a memory leak.  I do.  Am I using jME Physics incorrectly, or is there possibly a bug here in things not getting reset correctly?



Thanks everyone!


It is possible that there are some memory leaks in jME Physics 2 or even ODE - I never tested that. If you want to find such things, you'd need to use a memory profiler and find the "shortest path to gc root" for e.g. DynamicPhysicsNodes, Joints and stuff. If the memory leak is in ODE (so DynamicPhysicsNodes get collected but Bodies are not freed) it'll get more complicated, I'm afraid…

I tracked down a major source of the leak.  The physicsNode's geom's are cached in a static list here:



org.odejava.Geom.geomNativeAddr



When you do a physicsSpace.delete(), it cleans up a few things, but never this list.  By not freeing this list, references to pretty much everything stick around, and so nothing is ever deleted.  By doing a



org.odejava.Geom.geomNativeAddr.clear()



after i delete my physicsSpace, it triggered the deletion of pretty much everything.  So I can create/destroy my worlds with very little leakage.  I suspect there might be a little bit still, but its insignificant for my current purposes to worry.  Previously i was getting serious leaking.



This is with pre-jme phyics 2 release code…so maybe this was caught prior to the release, within the last month?

Another related thing…I modified OdePhysicsSpace.java

and added



        space.setChildCleanupMode(true);



to line ~325.



From the documentation, it sounded like it was a wise option to set for use with ODEJava.



Thoughts?

       

I changed Geom.geomNativeAddr to use weak references - does that solve your problem as well?

Not sure…haven't had a chance to checkout the latest CVS code.  In theory it should allow the objects to be reclaimed and eventually freed correctly…in practice, not sure…when we get a chance to test against the latest CVS code, i'll let you know.

Ive just checked out the CVS, I had the same error and it seems to be away.



thanks  :roll:


rasolfg said:

I tracked down a major source of the leak.  The physicsNode's geom's are cached in a static list here:

org.odejava.Geom.geomNativeAddr

When you do a physicsSpace.delete(), it cleans up a few things, but never this list.  By not freeing this list, references to pretty much everything stick around, and so nothing is ever deleted.  By doing a

org.odejava.Geom.geomNativeAddr.clear()

after i delete my physicsSpace, it triggered the deletion of pretty much everything.  So I can create/destroy my worlds with very little leakage.  I suspect there might be a little bit still, but its insignificant for my current purposes to worry.  Previously i was getting serious leaking.

This is with pre-jme phyics 2 release code...so maybe this was caught prior to the release, within the last month?



Can you please post the code that you used for the clean up?