Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Memory Leak with capture_array #1183

Open
Vanlon opened this issue Dec 23, 2024 · 3 comments
Open

[BUG] Memory Leak with capture_array #1183

Vanlon opened this issue Dec 23, 2024 · 3 comments

Comments

@Vanlon
Copy link

Vanlon commented Dec 23, 2024

Please only report one bug per issue!

I am using capture_array to capture an image to memory and then process it with opencv. After the image is captured and passed to another function it does not release it in memory. Every loop will add another variable to the memory that isn't released and eventually my Raspberry Pi 4 will run out of memory and kill the process.

Here is a dumbed down version of my code that reproduces the problem. I've added tracemalloc to identify where the memory usage stems from.

  from picamera2 import Picamera2
  import tracemalloc
  import time
  import cv2
  
  def takepic():
      
      tracemalloc.start()
  
      picam2 = Picamera2()
      preview_config = picam2.create_still_configuration(main={"size": (4000, 3000)})
      picam2.configure(preview_config)
      picam2.start()
      frame = picam2.capture_array()
      snapshot = tracemalloc.take_snapshot()
      top_stats = snapshot.statistics('lineno')
  
      for stat in top_stats[:2]:
          print(stat)
      
      #picam2.stop()
      picam2.close()
      resizeit(frame)
  
  def resizeit(img):
      small = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
      del img
      del small
      time.sleep(1)
      takepic()
      
  takepic()

I would expect that the variable gets released or overwritten every time the code loops instead of added to memory. Using free -m I can see the amount of memory decreasing every loop

image

image

I am using a Raspberry Pi 4B with 4Gb of ram and an ELP 13Mp autofocus camera

@davidplowman
Copy link
Collaborator

Hi, and thanks for the report.

I notice that your code calls takepic() recursively, meaning that the variables in the takepic stack frames might never be released. Does that sound plausible?

Perhaps the first thing to do is to rewrite the example slightly to avoid hanging on to those local variables. Is that something you could try, and then post back if you still have a problem? Thanks!

@Vanlon
Copy link
Author

Vanlon commented Jan 14, 2025

I've tried for a week to write this so it releases the local variable. In my program it's not recursive like this but still works similar with a trigger. If you have any insights that would be helpful.

@davidplowman
Copy link
Collaborator

Hi again, could you post a short example that reproduces the problem that you are experiencing? As I said, the memory problems in your previous example seem to be explainable, so it doesn't really help so much. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants