Finding memory leaks in solara with Memray and objgraph #172
maartenbreddels
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We noticed some memory leaks in solara, but the technique I've used to find them can be very useful in general for solara applications (or any Python program).
The first step is to isolate the issue. Let's first render a component, without any server:
We remove the reference to rc (the rendering context) and the container (
box)
, and run the garbage collector. I didn't expect that t manually running the garbage collector was needed, but we do need it for memray 🤷, which probably means I don't fully understand the lifecycle of memray and the Python process it runs.Now we run it under memray:
See https://bloomberg.github.io/memray/flamegraph.html for the
--leaks
argument.This gives us:
Where we see on the right that indeed, solara is not releasing some memory, zooming in we see this is using 11Mb of memory:
My guess here is that the
rc
(render context), has an extra reference to it. To check this, we use objgraphWe run the program (with or without memray, doesn't matter), and inspect
solara-mem-leak.png
:On the right, we see many internal references, but on the left we see the
rc
reference from our script and a reference from a thread-local object!.Fixing this in Reacton (the underlying library of solara) in widgetti/reacton@8ebc3a4 we see no more significant memory usage due to solara:
Hope this helps people debug memory leak issues! Ideas or improvements on this workflow are welcome 🙏
Beta Was this translation helpful? Give feedback.
All reactions