BUG: Memory leak or reference count problem on macOS
On macOS+Python+Qt, memory cannot be reclaimed properly when closing windows.
In this minimal example, the effect is small (tens to ~100 MB), but in practice it's a real problem because something about the references keeps other stuff around (data from the viz scene itself), so you can loose hundreds of MB of memory per window created. And in scripts people might want to make tens or hundreds of windows.
Minimal code using Python + VTK to open, show, and close 25 windows with gc.collect
in between:
Minimal example
import gc
import time
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QApplication, QFrame
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import vtk
class MainWindow(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.frame = QFrame()
self.vl = QVBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.vl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
source = vtk.vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.frame.setLayout(self.vl)
self.setCentralWidget(self.frame)
self.show()
self.iren.Initialize()
app = QApplication([])
for i in range(25):
print(i)
win = MainWindow()
win.show()
app.processEvents()
win.close()
app.processEvents()
del win
gc.collect()
time.sleep(0.5) # just to see individual bumps in mprof plot
On macOS (ARM/M1 + Python3.10 + VTK 9.1) I get:
$ mprof run rep.py && mprof plot
If I do 50 instead of 25 iterations I see this message sometimes:
...
48
Context leak detected, msgtracer returned -1
49
Not sure if the msgtracer
line is relevant, but it does show up. FWIW I doubt the exact versions matter here, since the original bug report was for macOS Intel + Python 3.9: https://github.com/mne-tools/mne-python/issues/10680 .
However, it seems to be macOS specific at least, since on Linux (Ubuntu 22.04) there is no such problem:
It's possible (but unlikely) that this could be related to #18559 (closed) if there is one underlying reference problem somewhere.
It's also possible that this is actually a PyQt problem, because if I comment out the VTK widget creation altogether, remove the time.sleep(0.5)
, and do 1000 iterations instead of 25, we can see slight memory usage creep on macOS (left) but not Linux (right):
macOS | Linux |
---|---|
Even if this does turn out to be a PyQt problem, it would be nice to work around it somehow!