[vtkusers] VTK memory management
José M. Rodriguez Bacallao
jmrbcu at gmail.com
Wed Aug 29 19:42:20 EDT 2012
so, I need to keep using vtk messenger from tvtk?
On Wed, Aug 29, 2012 at 7:14 PM, David Gobbi <david.gobbi at gmail.com> wrote:
> No, the tp_traverse function for PyVTKObject does not traverse the
> object's observers, so observer reference cycles will not be detected
> by the python garbage collector. This might be fixed in a later
> version of VTK, i.e. VTK six point something (or sooner if someone
> provides a patch).
>
> - David
>
> On Wed, Aug 29, 2012 at 4:28 PM, José M. Rodriguez Bacallao
> <jmrbcu at gmail.com> wrote:
>> with respect to reference cycles, some time ago I was using vtk
>> Messenger from TVTK to get rid of reference cycles when adding
>> observers to any vtk object, this is the reasoning from the source
>> code documentation in this file:
>>
>> """
>> Implements a simple, robust, safe, Messenger class that allows one to
>> register callbacks for a signal/slot (or event/handler) kind of
>> messaging system. One can basically register a callback
>> function/method to be called when an object sends a particular event.
>> The Messenger class is Borg. So it is easy to instantiate and use.
>> This module is also reload-safe, so if the module is reloaded the
>> callback information is not lost. Method callbacks do not have a
>> reference counting problem since weak references are used.
>>
>> The main functionality of this module is provided by three functions,
>> `connect`, `disconnect` and `send`.
>>
>> Here is example usage with VTK::
>>
>> >>> import messenger, vtk
>> >>> def cb(obj, evt):
>> ... print obj.__class__.__name__, evt
>> ...
>> >>> o = vtk.vtkProperty()
>> >>> o.AddObserver('ModifiedEvent', messenger.send)
>> 1
>> >>> messenger.connect(o, 'ModifiedEvent', cb)
>> >>>
>> >>> o.SetRepresentation(1)
>> vtkOpenGLProperty ModifiedEvent
>> >>> messenger.connect(o, 'AnyEvent', cb)
>> >>> o.SetRepresentation(2)
>> vtkOpenGLProperty ModifiedEvent
>> vtkOpenGLProperty ModifiedEvent
>> >>>
>> >>> messenger.send(o, 'foo')
>> vtkOpenGLProperty foo
>> >>> messenger.disconnect(o, 'AnyEvent')
>> >>> messenger.send(o, 'foo')
>> >>>
>>
>> This approach is necessary if you don't want to be bitten by reference
>> cycles. If you have a Python object holding a reference to a VTK
>> object and pass a method of the object to the AddObserver call, you
>> will get a reference cycle that cannot be collected by the garbage
>> collector. Using this messenger module gets around the problem.
>>
>> Also note that adding a connection for 'AnyEvent' will trigger a
>> callback no matter what event was generated. The code above also
>> shows how disconnection works.
>> """
>>
>> right now, has vtk solved this problem?
>>
>> On Wed, Aug 29, 2012 at 4:38 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>>> In general python's memory management is fairly deterministic because
>>> it relies on reference counting (i.e. most objects are deleted as soon
>>> as they aren't needed anymore). But if reference cycles are present,
>>> the behavior can become non-deterministic, because it depends on when
>>> the garbage collector is run. You can always call gc.collect()
>>> yourself to force the garbage collection to occur at certain times in
>>> your program.
>>>
>>> - David
>>>
>>>
>>> On Wed, Aug 29, 2012 at 2:03 PM, Thiago Franco Moraes
>>> <tfmoraes at cti.gov.br> wrote:
>>>> Hi,
>>>>
>>>> I'm doing some tests about memory management in VTK with Python. I
>>>> wrote this little python script [1]. It takes as parameter a filename
>>>> to a vtkPolydata saved in a xml file and do some processings in it. In
>>>> this script I'm taking care of freeing the memory used by each filter
>>>> and intermediate vtkPolydata. I'm using an tool called memory profiler
>>>> (very interesting by the way) [2] to measure the memory used along the
>>>> script execution. The problem is the memory measurement in the end of
>>>> script execution is somewhat strange, because the memory measurement
>>>> is very fluctuating, ranging from 170MB to 500MB. Is this expected?
>>>> I'm freeing memory from vtk objects in the right way? It's a problem
>>>> with Python?
>>>>
>>>> Thanks!
>>>>
>>>> [1] - http://pastebin.com/iYUK1jxk
>>>> [2] - https://github.com/fabianp/memory_profiler
>>> _______________________________________________
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>
>>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.vtk.org/mailman/listinfo/vtkusers
More information about the vtkusers
mailing list