You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using useFragment instead of createFragmentContainer, in an SSR context the resourceMap holds onto the environment longer than it needs too, which causes the application to look like its got a memory leak, as the store is contained inside the environment. As touched on #2854 (comment)
I will prefix that with being, that in an SSR setting, you'd ideally want to blow away the store on every request, so you're not leaking store data between requests. So this only came about, when you create a new Environment, for the request. And having the resourceMap Map hold onto now discarded environments.
Summary:
With this PR I aim to address the situation where environments are removed, but never garbage collected as references are held onto in this Map.
A simple case could be:
```js
const MyApp = () => {
const [environment, setEnvironment] = useState(new Environment({ network, store }));
useEffect(() => {
setTimeout(() => {
setEnvironment(new Environment({ network, store }));
}, 1e3);
}, []);
return (<RelayEnvironmentProvider environment={environment}>
<MyComponentTreeWith_A_useFragment/>
</RelayEnvironmentProvider>);
};
```
as you'll find from that example, the `dataResources` map will be forever growing, with nothing clearing.
fixes: #3013
Pull Request resolved: #3014
Reviewed By: jstejada
Differential Revision: D19797050
Pulled By: alunyov
fbshipit-source-id: 4ef18e7634c7d50597944967e9ffc0a330ac3633
When using useFragment instead of createFragmentContainer, in an SSR context the resourceMap holds onto the environment longer than it needs too, which causes the application to look like its got a memory leak, as the store is contained inside the environment. As touched on #2854 (comment)
I will prefix that with being, that in an SSR setting, you'd ideally want to blow away the store on every request, so you're not leaking store data between requests. So this only came about, when you create a new Environment, for the request. And having the resourceMap Map hold onto now discarded environments.
relay/packages/relay-experimental/FragmentResource.js
Lines 440 to 451 in 02d9e71
My suggestion is to move away from a Map, and into a WeakMap, so that once the environment gets gc'd, it cleans up the fragment resourceCache.
The text was updated successfully, but these errors were encountered: