[BUG] Memory Leak when reinitializing Picamera2 with new camera_num, results in OSError: [Errno 12] Cannot allocate memory
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from raspberrypi/picamera2
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
raspberrypi/picamera2#1405 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
raspberrypi/picamera2#1404 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 57/100
raspberrypi/picamera2#1403 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
raspberrypi/picamera2#1392 · 13 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
raspberrypi/picamera2#1389 · 4 comments ·
All issues in raspberrypi/picamera2
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100