[vtkusers] memory usage with python+vtk

David Gobbi dgobbi at irus.rri.ca
Fri Jun 7 10:15:01 EDT 2002


On Fri, 7 Jun 2002, white colin john wrote:

> My question is, does setting the variable referencing a vtk object to None
> definitely reclaim the resources used?  Is there some interface in python
> for destroying these objects which is more explicit, such as that used in
> the tcl example?  Any advice is welcome.

The most explicit way of deleting a reference in python is with the 'del'
statement, e.g.

del cone

This will decrement the reference count of the python name 'cone' and,
if the resulting reference count is zero, it will go on to decrement
the reference count of the VTK object that 'cone' points to.  This
is only guaranteed to destroy the VTK object if the reference counts
of both the python name 'cone' and the VTK object were both exactly 1.

Also, if you call 'del' on a global variable in python, the variable
is usually destroyed a couple code lines after the 'del' statement,
just because of the way the python interpreter works.

The best way to ensure that objects are destroyed in python is to
avoid the use of the global namespace.  Put all your code inside
of the bodies of functions or methods so that the scope of each
variable is clearly defined.  When the function/method terminates
all local variables go out of scope and their reference counts are
reliably decremented.

Cheers,

 - David

--
  David Gobbi, MSc                       dgobbi at irus.rri.ca
  Advanced Imaging Research Group
  Robarts Research Institute, University of Western Ontario






More information about the vtkusers mailing list