Diagnosing a potential memory leak?
-
The program I'm running proceeds without issue for several minutes (probably close to 10 minutes, I haven't done a closely monitored test of when the error arises). Anyway, after running for a period of time, I am give a memory allocation error of 4096 bytes.
while(1): time.sleep(2) # sleep for two seconds as per DHT specifications result = th.read() # read values from DHT if result.is_valid(): pycom.rgbled(0x001000) # green - onboard data read confirmation print('Temperature: {:3.2f}'.format(result.temperature/1.0)) # print temp to console print('Humidity: {:3.2f}'.format(result.humidity/1.0)) # print rh to console full, ir = tsl.get_full_luminosity() # read raw values (full spectrum and ir spectrum) lux = tsl.calculate_lux(full, ir) # convert raw values to lux print('Lux: ' + str(lux) + ' Lumens') display = 'Temperature: {:3.2f}\nHumidity: {:3.2f}\nLux: {:3.2f}'.format(result.temperature/1.0,result.humidity/1.0,lux) lcd.clear() lcd.putstr(display) gc.mem_free() # memory management gc.collect()
The only code I haven't included from main.py is the initialization of my pins.
If required, I can post the code from my imported library that pinpoints the exact function where the memory allocation error arises. However, since the program does run for a time, I don't think it's an issue with any specific function, but rather that I am not handling my memory efficiently as it depletes over time.
-
Never mind, this merely delayed the issue I was having - which extended my run time from around 10 minutes to several hours. It was able to run without issue for quite a while (I left it overnight) and at some point during the night I encountered the same memory allocation error.
If anyone has any insight into how I might be able to remedy this, I would really appreciate it.
-
Figured I'd update and answer this question in case anyone runs into a similar issue, the problem was very simply solved by deleting my variables at the end of the while loop:
del result, full, ir, display