[vtkusers] release memory allocated in vtk from python
David Gobbi
david.gobbi at gmail.com
Mon Jul 2 14:36:49 EDT 2012
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