Recoil: Memory leak

I’m sure an example can be simplified, but somewhere between 0.0.7 and 0.0.10 memory leak were introduced.

import React from "react";
import { RecoilRoot, useSetRecoilState, atom, useRecoilValue } from "recoil";

const state = atom({
  key: "test",
  default: []
});

const Subscriber = () => {
  const setTest = useSetRecoilState(state);

  React.useEffect(() => {
    const u = old => {
      const item = Math.random();

      if (old.length >= 5000) {
        return [item, ...old.slice(0, -1)];
      } else {
        return [item, ...old];
      }
    };

    const t = () => setTest(u);
    setInterval(t, 10);
  }, []);

  return null;
};

const Test = () => {
  const test = useRecoilValue(state);
  return <div>{test.length}</div>;
};

export default function App() {
  console.log("render");
  return (
    <RecoilRoot>
      <Subscriber />
      <Test />
    </RecoilRoot>
  );
}

Codesandbox:
0.0.10: code, demo
0.0.7: code, demo

Sandbox with 0.0.7 version doesn’t have memory leak, as you might see here (app run about 2 minutes between heap snapshots). 0.0.10: image 0.0.7:
image

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (6 by maintainers)

Most upvoted comments

We’re finalizing some changes for Concurrent Mode now, then can try to investigate this before the next release.

@saltycrane - Thank you for the reproducer.

In case it’s helpful, here is a sample webapp with code that demonstrates symptoms of a memory leak:

Webapp: https://saltycrane.github.io/recoil-vs-context-grid-test/recoil?x=200&y=100 Repo: https://github.com/saltycrane/recoil-vs-context-grid-test

This is fixed in master, which we plan to release tomorrow. Please note that development builds will leak memory by pushing onto window.$recoilDebugStates. You can delete from this array if it causes you problems.

could you please make this more prominent in the documentation? thanks for the otherwise great lib 😃

@drarmstr any ETA on the next release? we’re heavily dependant on Recoil and plan to migrate a huge application from redux to recoil. Thank you for creating a missing piece of React ❤️

Pretty soon, actually!

This is fixed in master, which we plan to release tomorrow. Please note that development builds will leak memory by pushing onto window.$recoilDebugStates. You can delete from this array if it causes you problems.

@Nishchit14 I’ve myself migrated app to focal It has similar concepts and is battle-tested in production at Grammarly.

Give it a try.