Closed wattsjs closed 4 months ago
Sorry I should have mentioned - the reproduction uses the $derived
in the component (causing the leak), which when changed from newData
to state.newData
will behave as expected:
https://github.com/wattsjs/svelte-derived-memory-leak/blob/main/src/lib/SomeComponent.svelte#L28
Thanks for the quick fix @trueadm 🙏
Describe the bug
I'm not sure if this is my misunderstanding of the DOM + component lifecycle, however if this is not a bug I would say it is a nasty footgun and can lead to fairly large memory leaks if dealing with large data structures.
I am storing some large amount of data loaded from an external API in a class based state, and then running some derivations on this state as needed. This state is fetched for each different page and then updated.
For example something like below:
If I have this
$derived
inside of the class itself (or even in the/[id]/+page.svelte
), all works fine. But there are some cases in my app that I want to run this derivation inside of a component, for example if I have many items and want to derive some data from each item. When I move the$derived
inside of the component, the memory does not clean up properly as I assume it is still referencing the$state
fromAppState
, which exists - although the data has now changed.For example some Chrome memory allocation timelines, we can see that when the
$derived
rune is inside of the component the memory does not get released on each page navigation (vertical line), whereas it does when it is inAppState
:$derived
in the state class (or +page):$derived
inside the component:Is this intended behaviour? I would like to have the flexibility to add
$derived
runes inside of my nested components to calculate new state that is only required locally, instead of having to derive it at a higher level and pass it down (which would be tricky when dealing with lists or maps)Reproduction
https://github.com/wattsjs/svelte-derived-memory-leak
Logs
No response
System Info
Severity
blocking an upgrade