Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • VTK VTK
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 738
    • Issues 738
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 201
    • Merge requests 201
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • VTK
  • VTKVTK
  • Issues
  • #17557

Closed
Open
Created Mar 20, 2019 by Fx III@fxiiii

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")

memory

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
Assignee
Assign to
Time tracking