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.
-
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 -
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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login