Garbage Collection

Hi there, I have some doubts about the garbage collection:

If I detach (using node.detach()) a panda node from the scene and lose all references to it, does the panda node (and all it’s children) get garbage collected automatically or I have to explicitly destroy the node with node.removeNode()?

Same question for Textures, Models and Actor Objects. From the manual I need to call delete() on Actors. Do I need to do the same for Textures and Models?

Any tips that are not in the manual already: … _Instances

would be appreciated too.

In general, node.detach(), and losing all references, will be sufficient to delete the node. But you do have to make sure you lose all references, and Python doesn’t necessarily make this easy–sometimes you’re holding onto a reference you’re not thinking about.

This is why you have to explicitly call Actor.delete(), because otherwise some of the hooks that Actor creates for itself would result in outstanding references.

In general, low-level Panda objects don’t create hooks for themselves, so you don’t need to do this for Textures and generic models (e.g. NodePath objects).


One handy debugging tool is the gc module which finds all the references to something.

import gc
for ref in gc.get_referrers(something):
    print ref

First thanks for the responses.

Thanks, that should be useful for debugging :slight_smile:

drwr, does panda create any references I should worry about? I come from a ANSI C background so I’m used to watching over my pointers, but I can only watch the ones I create myself.

Also what if I want to unload a texture from memory to make room for other textures, like when changing the level of a game? Losing the reference and detaching all nodes that use that texture is enough?

Generally, Panda doesn’t create references to your Python objects. There are a few things that do, for instance, self.accept() creates a new reference to self in the messenger table.

There is a rendering cache that can keep around pointers to certain objects that have recently been rendered, but not indefinitely. Things will eventually rotate out of the cache by themselves.

Also, Panda keeps an extra reference to every texture you load via loader.loadTexture() (and every model via loader.loadModel() too), so it can unify reloads of the same filename multiple times. You can use loader.unloadTexture() to remove this extra reference. Once you have unloaded the texture, removing all references to it will be sufficient to delete the texture.