I believe I’ve found a memory leak in Klipper. About 2.5 hours into a 4h print yesterday Klippy was killed by the Linux OOM killer, after using about 400MB. Unfortunately I didn’t save the dmesg buffer before rebooting, but those things are pretty inscrutable anyway. I’ve been able to confirm and reproduce the memory growth condition using klippy’s batch mode, with the help of memory_profiler and scalene, on both python 2.7.16 and 3.8.0.
A plot from memory_profiler using the attached gcode file looks like this:
profiler setup
~/klippy-env/bin/pip install memory-profiler scalene
repro steps
The following resources are contained in the attached zip file:
-
failed-job.gcode
– print job that triggered the OOM -
firmware-btt-octopus-11.dict
,huvud.dict
– dict files from my two MCUs -
processed-from-log.cfg
– single config file captured from my klipper install, modified slightly to work with batch mode
repro.zip (6.4 MB)
memory_profiler
This’ll show the memory growth over time; it generates plots like the one above.
~/klippy-env/bin/mprof run ~/klippy-env/bin/python \
/home/klipper/klipper/klippy/klippy.py \
processed-from-log.cfg \
-i failed-job.gcode \
-o test.serial \
-d firmware-btt-octopus-11.dict \
-d huvud=huvud.dict
Generate the plot with mprof plot
.
scalene
I used scalene with klipper in a python3 venv; I didn’t try to use it with python2, but it may be compatible if installing from the repo (see scalene#358).
~/klippy-env/bin/scalene --outfile scalene-klippy.json --json \
/home/klipper/klipper/klippy/klippy.py \
processed-from-log.cfg \
-i failed-job.gcode \
-o test.serial \
-d firmware-btt-octopus-11.dict \
-d huvud=huvud.dict
Use the scalene gui to view the results of the profiler run. You can use the data from one of my profiler runs, to get a feel for what’s available: scalene-klippy.zip (contains scalene-klippy.json
)
postulation
It looks to me like MCU_stepper.generate_steps
is leaking memory.