pimoroni / EnviroPlus-FeatherWing

CircuitPython library for the Enviro+ FeatherWing
https://shop.pimoroni.com/products/enviro-plus-featherwing
MIT License
10 stars 11 forks source link

plotters_combined with PMS5003 blows up after a few minutes with MemoryError in update - replace with fixed size, circular buffer #19

Open kevinjwalters opened 3 years ago

kevinjwalters commented 3 years ago

The example https://github.com/pimoroni/EnviroPlus-FeatherWing/blob/master/examples/plotters_combined.py is robust wrt memory usage until the PMS5003 is added on a Feather nRF52840 Express. With that additional sensor and associated graph it then blows up after about 15 minutes with a MemoryError:

checksum error
checksum error
Traceback (most recent call last):
  File "code.py", line 209, in <module>
  File "lib/pimoroni_envirowing/screen/plotter.py", line 103, in update
MemoryError: memory allocation failed, allocating 1288 bytes

Press any key to enter the REPL. Use CTRL-D to reload.

Assuming this isn't a leak there's probably some efficiency measure that can improve the situation here. Pre-allocation of self.data_points and storing number of valid entries would probably help. This is the approach taken here with an implementation of a circular buffer for the width of screen plus pending extra_data awaiting being drawn via draw() (plus 1 based on how the implementation works).

A crude fix might be to allow the width of plot to be specified and simply reduce it from the default 160.

kevinjwalters commented 3 years ago

Test this with interval 0.2 and passive mode to get the plot zipping along.

kevinjwalters commented 3 years ago
kevinjwalters commented 3 years ago
kevinjwalters commented 3 years ago

After auto_discard addition, Feather nRF52840 Express on 6.0.0:

Four lines, 3 ramping took 20.0684 seconds
Four lines, 3 ramping took 15.576 seconds
Three flat lines took 16.8184 seconds
Three flat lines took 15.1393 seconds
Two lines, few draws took 13.2486 seconds
Two lines, few draws took 12.5482 seconds
Alternating, missing took 13.6402 seconds
Alternating, missing took 11.7799 seconds
kevinjwalters commented 3 years ago

plotters_combined (00e159014dd3b51120b3783ec83452e30abe8424) now runs better on the Feather nRF52840 Express with PMS5003 attached. Goes from 53648 bytes free at end of updates to steady state of around 31088 bytes.