How can I detect and verify a memory leak in Flask's request context caused by uncleaned g data?
0 reputation · 06 Jan 2022, 17:59 UTC
I want to determine whether storing large temporary objects in the Flask `g` proxy for each request leads to a memory leak, and how to confirm that a fix using `teardown_appcontext` actually releases those objects. The goal is to observe memory growth over many requests and ensure it stabilizes after the cleanup.
I am working with a standard Flask application that uses the request context and the `g` object, and I can reproduce the scenario locally. I am unsure which profiling or monitoring tools reliably show per‑request memory usage in Flask, and how to assert that the context is properly torn down so that no residual references remain.
What tools or techniques can be used to monitor memory usage per request in Flask? How can I verify that `g` is cleared after each request? Is there a reliable way to assert that no objects remain in the context after the teardown callback?