[BUG] Memory Leak when reinitializing Picamera2 with new camera_num, results in OSError: [Errno 12] Cannot allocate memory

Open
#958 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
embedded-iot

Research direction

Start with the reproduction loop and trace Picamera2.start() and configure_() in picamera2.py into dmaallocator.py and dma_heap.py, where allocation fails. Run the script while monitoring memory and DMA-heap usage, then determine whether repeated close/reinitialization releases resources. Done means repeated camera changes no longer grow memory indefinitely or reach OSError: [Errno 12].

Written by the indexing model from the issue text.

Description

Describe the bug
Every time I initialize Picamera2 with a new camera_num, more memory seems taken by the Picamera2 object. Eventually the system runs out of memory and I get a OSError: [Errno 12] Cannot allocate memory error.

To Reproduce

from picamera2 import Picamera2, Preview
import time

while True:
    for i in range(1):
        picam2 = Picamera2(camera_num=i)
        picam2.start_preview(Preview.NULL)
        picam2.start()
        picam2.capture_file("device/runtime/picture.jpg")
        picam2.close()
    time.sleep(0.1)

Expected behaviour
Expected that this script could run forever without an error.

Console Output, Screenshots

Traceback (most recent call last):
  File "/home/myles/dev/zeebo/device/picam2_memoryleak.py", line 8, in <module>
    picam2.start()
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1149, in start
    self.configure(config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1104, in configure
    self.configure_(camera_config)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1087, in configure_
    self.allocator.allocate(libcamera_config)
  File "/usr/lib/python3/dist-packages/picamera2/allocators/dmaallocator.py", line 43, in allocate
    fd = self.dmaHeap.alloc(f"picamera2-{i}", stream_config.frame_size)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/picamera2/dma_heap.py", line 98, in alloc
    ret = fcntl.ioctl(self.__dmaHeapHandle.get(), DMA_HEAP_IOCTL_ALLOC, alloc)

Hardware :
RPi 4. Gen3 wide angle and Gen2 camera connected to an Arducam Camera Board V2.2

Additional context

Dominant language
Python
Stars
1.2k
Forks
258
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from raspberrypi/picamera2

All issues in raspberrypi/picamera2

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.