Skip to content

Memory leak in a webserver? #135

Answered by abey79
abey79 asked this question in Q&A
May 23, 2022 · 5 comments · 3 replies
Discussion options

You must be logged in to vote

It looks like gc.collect() forces memory release, and it appears that PIL is the culprit. Here is a simplified demo script, without FastAPI.

import gc
import io

import numpy as np
import psutil
import PIL
from picamera2 import Picamera2

picam2 = Picamera2()
still_config = picam2.still_configuration(controls={"ExposureValue": 0})
picam2.configure(still_config)
picam2.start()


def get_pic() -> bytes:
    array = picam2.capture_array()
    image = PIL.Image.fromarray(array)
    data = io.BytesIO()
    image.save(data, format="JPEG")
    data.seek(0)
    buffer = data.read()
    return buffer


def get_pic2() -> np.ndarray:
    array = picam2.capture_array()
    return array


print("With …

Replies: 5 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by abey79
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@davidplowman
Comment options

@davidplowman
Comment options

@abey79
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants