[vtkusers] release memory allocated in vtk from python

David Gobbi david.gobbi at gmail.com
Mon Jul 2 16:58:32 EDT 2012


That's correct.  ShallowCopy() will not increment the reference
count of the reader.  It will only increment the reference counts
of the arrays in the reader's output. It also copies information like
the Origin and Spacing.

CopyStructure() will copy even less: it only copies the information,
not the arrays.

 - David

On Mon, Jul 2, 2012 at 2:16 PM, José M. Rodriguez Bacallao
<jmrbcu at gmail.com> wrote:
> ok, but with a shallow copy, the output will not increment the reader reference?
>
> On Mon, Jul 2, 2012 at 4:02 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>> A shallow copy isn't like a weakref.  A shallow copy will copy all the
>> arrays from one data object to another data object by reference
>> (strong reference, not weak reference).  A deep copy will create new
>> arrays and copy all the values from the old arrays.
>>
>> I'm not sure how much git master will change before it is released, if
>> that's what you are asking, but it already has the new pipeline
>> features.
>>
>>  - David
>>
>> On Mon, Jul 2, 2012 at 1:26 PM, José M. Rodriguez Bacallao
>> <jmrbcu at gmail.com> wrote:
>>> so, a shallow copy is like a weakref in python no? I don't want to be
>>> that minimalist (example 2) because example 2 does not copy any lookup
>>> table, etc. How far is the git/master from what would be VTK 6.0?
>>>
>>> On Mon, Jul 2, 2012 at 2:36 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>>>> The original question was asking how to free the reader and keep the
>>>> data.  When you do "output=reader.GetOutput()", the output keeps a
>>>> reference to the reader, so the reader will not be freed until the
>>>> output is freed.  This is why it sometimes make sense to copy the
>>>> output into a new vtkImageData object.
>>>>
>>>> In VTK 6, if you call "output=reader.GetOutput()" then the output does
>>>> not keep a reference to the reader, so memory management is a bit
>>>> easier.
>>>>
>>>> Python memory management does what it's supposed to: it deletes
>>>> objects that aren't referred to by any other objects.  If an object is
>>>> part of a pipeline, it won't be deleted until 1) it is disconnected
>>>> from the pipeline or 2) there are no external references to any of the
>>>> objects that make up the pipeline.
>>>>
>>>>  - David
>>>>
>>>>
>>>> On Mon, Jul 2, 2012 at 12:12 PM, Jothybasu Selvaraj <jothybasu at gmail.com> wrote:
>>>>> Won't the auto garbage collection/ memory management in python manage this?
>>>>>
>>>>>
>>>>> On Mon, Jul 2, 2012 at 7:05 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>>>>>>
>>>>>> A shallow copy should be sufficient:
>>>>>>
>>>>>> def test(filename):
>>>>>>    r = vtkgdcm.vtkGDCMImageReader()
>>>>>>    r.SetFileName(filename)
>>>>>>    r.Update()
>>>>>>    img = vtk.vtkImageData()
>>>>>>    img.ShallowCopy(r.GetOutput())
>>>>>>    return img
>>>>>>
>>>>>> Or, to be even more minimalist, copy just the pixel data and nothing else:
>>>>>>
>>>>>> def test(filename):
>>>>>>    r = vtkgdcm.vtkGDCMImageReader()
>>>>>>    r.SetFileName(filename)
>>>>>>    r.Update()
>>>>>>    img = vtk.vtkImageData()
>>>>>>    img.CopyStructure(r.GetOutput())
>>>>>>    img.GetPointData().PassData(r.GetOutput().GetPointData())
>>>>>>    return img
>>>>>>
>>>>>>  - David
>>>>>>
>>>>>> On Mon, Jul 2, 2012 at 10:17 AM, Jothy <jothybasu at gmail.com> wrote:
>>>>>> > I think the first one.
>>>>>> >
>>>>>> > Jothy
>>>>>> >
>>>>>> > On 02-Jul-2012, at 5:06 PM, José M. Rodriguez Bacallao
>>>>>> > <jmrbcu at gmail.com> wrote:
>>>>>> >
>>>>>> >> hi folks, how to correctly release memory using vtk from python
>>>>>> >> for example, which one of this three code snnipets release the memory
>>>>>> >> allocated when reading the file:
>>>>>> >>
>>>>>> >> def test(filename):
>>>>>> >>    r = vtkgdcm.vtkGDCMImageReader()
>>>>>> >>    r.SetFileName(filename)
>>>>>> >>    r.Update()
>>>>>> >>    img = vtk.vtkImageData()
>>>>>> >>    img.DeepCopy(r.GetOutput())
>>>>>> >>    return img
>>>>>> >>
>>>>>> >> --------------------------------------------------------------------------
>>>>>> >> def test(filename):
>>>>>> >>    r = vtkgdcm.vtkGDCMImageReader()
>>>>>> >>    r.SetFileName(filename)
>>>>>> >>    r.Update()
>>>>>> >>    return r.GetOutput()
>>>>>> >>
>>>>>> >> ---------------------------------------------------------------------------
>>>>>> >> r = vtkgdcm.vtkGDCMImageReader()
>>>>>> >> r.SetFileName(filename)
>>>>>> >> r.Update()
>>>>>> >> img = r.GetOutput()
>>>>>> >> img = None
>>>>>> >> del img
>>>>>> >> r = None
>>>>>> >> del r



More information about the vtkusers mailing list