Python NewCellIterator leak
Expected Behavior
iterating cell using polydataInstance.NewCellIterator() multiple times should increase the memory usage linearly
Current Behavior
the iterator seams to retain the allocated memory even when the python wrapper goes out of scope
Possible Solution
The wrapper destructor should deallocate the wrapped memory
Steps to Reproduce
import urllib
import os
import psutil
import vtk
import time
# download sample data
if not os.path.exists("./42400-IDGH.stl"):
open("./42400-IDGH.stl", "wb").write(
urllib.urlopen("https://github.com/naucoin/VTKData/raw/master/Data/42400-IDGH.stl").read())
def iterateOverCells(dataset):
iterator = dataset.NewCellIterator()
while not iterator.IsDoneWithTraversal():
iterator.GoToNextCell()
def iterateOverCellsInefficient(dataset):
for i in range(dataset.GetNumberOfCells()):
dataset.GetCell(i)
reader = vtk.vtkSTLReader()
reader.SetFileName("./42400-IDGH.stl")
reader.Update()
process = psutil.Process(os.getpid())
efficient = []
inefficient = []
start = time.time()
for i in xrange(1000):
inefficient.append(process.memory_info().rss)
iterateOverCellsInefficient(reader.GetOutput())
print time.time() - start
start = time.time()
for i in xrange(1000):
efficient.append(process.memory_info().rss)
iterateOverCells(reader.GetOutput())
print time.time() - start
import matplotlib as mpl
mpl.use("Agg")
from matplotlib import pyplot as plt
toMB = lambda d: d / 1024. / 1024.
plt.plot(map(toMB, inefficient), label="inefficient")
plt.plot(map(toMB, efficient), label="efficient")
plt.legend()
plt.title("memory usage (MB)")
plt.show()
plt.savefig("memory.png")
Context (Environment)
Python 2.7.15 (default, Jan 12 2019, 22:20:12)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
backports.functools-lru-cache==1.5
cycler==0.10.0
kiwisolver==1.0.1
matplotlib==2.2.4
numpy==1.16.2
psutil==5.6.1
pyparsing==2.3.1
python-dateutil==2.8.0
pytz==2018.9
six==1.12.0
subprocess32==3.5.3
vtk==8.1.2