Can't figure out why I'm getting memory leak

46 views
Skip to first unread message

Eric Afyouni

unread,
Feb 23, 2017, 2:15:19 AM2/23/17
to pyqtgraph
Hi,

I've just recently started learning Python and wrote this code to plot temperature data coming from an Arduino Uno over serial to a Raspberry Pi. I've been researching and trying different methods to try and stop the memory leak, but nothing seems to work. I'm not sure if the issue is with plotting or collecting the data. I've slowed down the accumulation, but at this point I need help figuring out what I have done wrong. Any help that can be provided is great. 

import re
import datetime
import pyqtgraph as pg
from PyQt5 import QtCore,QtGui
import numpy as np
import serial
import csv

ser = serial.Serial('/dev/ttyACM0', 9600)

teml = []
numeric_const_pattern = r'[-+]?(\d+\.\d+|\d+)'

def parser(command):
    
    first_bit = re.findall(r'\b[A-Za-z]+',command)
    command_value = re.findall(numeric_const_pattern,command)
    
    decision(first_bit, command_value)

def decision(letter, number):

    cmd_bits = 'Temp', 'X', 'A'
     
    if cmd_bits[0] in letter:
        Temperature_plotting(number[0])
        del number[:]
    else:
        pass
        
def Temperature_plotting(temperature):
    
    ctim = datetime.datetime.now().time()    
    resetim = datetime.time(0, 0, 10, 0)

    teml.append(float(temperature))

    if len(teml) >= 1000:
        
        with open('daily_temp_data.csv', 'a') as csvfile:
            fieldnames = ['Date Stamp','Max','Min']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            x = datetime.date.today()
            max_temp = max(teml)
            min_temp = min(teml)
            writer.writerow({'Date Stamp': x,'Max': max_temp,'Min': min_temp})
    
        del teml[:]

    return(teml)

   
pw = pg.plot(clear=True)
pw.setLabel('left', text='Temp', units='F')
pw.setLabel('bottom', text='Time', units='n*5sec')
pw.setTitle('Temperature')
pw.showGrid(True, True)
pw.showLabel('left', True)
pw.showLabel('bottom', True)
pw.showLabel('top')
pw.setYRange(20, 100, padding=0)
           
while True:
    
    if ser.inWaiting:
        cmd = str(ser.readline())
        parser(cmd)
        ser.flushInput()
        
    pw.plot(teml[:-1], clear=True)

    pg.QtGui.QApplication.processEvents()

vas...@gmail.com

unread,
Feb 23, 2017, 3:41:53 AM2/23/17
to pyqt...@googlegroups.com
In same window (pg.plot() is pyqtgraph.graphicsWindows.PlotWindow) you initialize and plot new and new pyqtgraph.graphicsItems.PlotDataItem with pw.plot(). It is better to initialize PlotDataItem first (pl = pw.plot()), then just set the data it with setData(). Also your way of using of teml variable is suspicious, but there isn't memory leak currently.

Cheers,
Vasilije

#Leak:
import pyqtgraph as pg

pw = pg.plot(clear=True)
pw.setLabel('left', text='Temp', units='F')
pw.setLabel('bottom', text='Time', units='n*5sec')
pw.setTitle('Temperature')
pw.showGrid(True, True)
pw.showLabel('left', True)
pw.showLabel('bottom', True)
pw.showLabel('top')
pw.setYRange(20, 100, padding=0)
teml=[21,22,43,32]
          
while True:      
    pw.plot(teml, clear=True)
    pg.QtGui.QApplication.processEvents()




#No leak:
import pyqtgraph as pg
pw = pg.plot()
pl = pw.plot()

pw.setLabel('left', text='Temp', units='F')
pw.setLabel('bottom', text='Time', units='n*5sec')
pw.setTitle('Temperature')
pw.showGrid(True, True)
pw.showLabel('left', True)
pw.showLabel('bottom', True)
pw.showLabel('top')
pw.setYRange(20, 100, padding=0)
teml=[21,22,43,32]
          
while True:      
    pl.setData(teml, clear=True)
    pg.QtGui.QApplication.processEvents()




On Thu, Feb 23, 2017 at 8:15 AM, Eric Afyouni <eafy...@gmail.com> wrote:
import pyqtgraph as pg


Eric Afyouni

unread,
Feb 24, 2017, 4:49:42 PM2/24/17
to pyqtgraph
Thank you very much for your response. I will make these changes to the code asap. As for the list teml, I agree it is not implemented well. I plan to improve the code as soon as I make these changes.
Reply all
Reply to author
Forward
0 new messages