jminardi/mecode

Memory leak in serial mode

dreammaker opened this issue · 1 comments

The following loop that simply moves back and forth will slowly consume more and more memory, presumably until it crashes.

g = G(direct_write_mode='serial')
while True:
    g.move(10, 0)
    g.move(-10, 0)

The problem is that the Printer class is storing every line ever sent in _buffer and doesn't clear it out until disconnecting.

Conservatively assuming 20 characters per G-code command, 1,000,000 G-code commands would require roughly 72 + (37+20*7+8 )*1000000 bytes = 185000000 bytes = 185 MB for _buffer and another 185MB for the responses list (for calculation see https://code.tutsplus.com/tutorials/understand-how-much-memory-your-python-objects-use--cms-25609 ). If it is possible to replace those lists with ring buffers, that would be a nice change.