1

I need to take a picture in each second in an infinite loop. I've wrote 2 different python scripts: one with camera.capture() and another with camera.capture_continuous()

In both cases, I see the Buff/Cache memory keeps increasing until the maximum possible (after ~30 minutes).

When I stop the script, the memory stops increasing but doesn't decrease. The only way I've found to decrease the buff/cache memory to a level similar to before launching the script is to remove all the created jpeg files.... Do you have an idea from where is the memory leak? Is it already known?

Here is my code:

with PiCamera() as camera:
    # Picamera object / objet Picamera
    camera.resolution = (cmd['camera_resolution'][0],cmd['camera_resolution'][1])
    camera.iso = cmd['camera_iso']
    camera.start_preview()
    time.sleep(2)
    try:
        for i, image_filename in enumerate(camera.capture_continuous('image_{counter}.jpg', format='jpeg')):
        time.sleep(cmd['sleep_time'])
            if os.path.isfile(cmd['stopfile']):
                break
        camera.stop_preview()
        camera.close()
        sys.exit(0)
    except KeyboardInterrupt:
        camera.stop_preview()
        camera.close()
        print ('Quit')
  • If I wrote a script which used more and more memory I'd assume I had an error in the script. – joan Feb 24 at 22:56
  • I've edited my post with my code. Do you see any error @joan? – jsec83 Feb 25 at 10:28
  • I am not a Python guy but in C# this type of problem is handled by garbage collection. It seems like you are allocating thing to memory.. and then never releasing it. Hence it keeps growing and growing. Maybe this can help ?? digi.com/resources/documentation/digidocs/90001537/references/… – Piotr Kula Feb 25 at 10:40
  • @ppumkin Thanks for your answer. Ok I agree with you about garbage collector. but I don't know how can free the memory with the usual piCamera syntax : with PiCamera() as camera: – jsec83 Feb 25 at 14:42
  • Well from what I just read about with it is like the using statement in C# - meaning once you are done with the code inside there... it should automatically dispose of it (letting GC know the memory can be free'd up) so maybe its not that with causing the problem. Again.. I dont know much about Python lifecycle and how it deals with stuff inside the with - in C# anything inside the using get marked disposed before the using is disposed. Maybe it is PiCamera not behaving properly and instead of getting disposed it hangs around doing something "magic" - who knows. play around a bit – Piotr Kula Feb 25 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.