question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

memory leak in useDrawCallback

See original GitHub issue

related to #625

Any closure dependency of useDrawCallback lives forever. The draw callback function does appear to be garbage collected, however.

Steps to reproduce

this is the example code

export const SkiaGradientImp = ({
  width,
  height,
}: {
  width: number;
  height: number;
}) => {
  const paint = Skia.Paint();
  const onDraw = useDrawCallback((canvas) => {
    // Cyan Circle
    paint.setShader(
      Skia.Shader.MakeLinearGradient(
        Skia.Point(0, 0),
        Skia.Point(width, height),
        ["red", "cyan", "lime"].map((color) => Skia.Color(color)),
        null,
        TileMode.Clamp
      )
    );
    canvas.drawRect(Skia.XYWHRect(0, 0, width, height), paint);
  });
  return <SkiaView style={{ width, height }} onDraw={onDraw} />;
};

export const PerformanceGradients = () => {
  const [index, setIndex] = useState(0);
  return (
    <TouchableWithoutFeedback onPress={() => setIndex((i) => i + 1)}>
      <View style={{ flex: 1, flexDirection: "row", flexWrap: "wrap" }}>
        {new Array(300).fill(0).map((_, i) => (
          <SkiaGradientImp key={`${index}-${i}`} width={30} height={30} />
        ))}
      </View>
    </TouchableWithoutFeedback>
  );
};

If you press the button, you will have 300 views in memory but 600 paints objects. If paint is not a closure dependency (we move it’s creation in the callback), it appears to be deleted properly. test

In the case of the renderer, the dependencies to the closure are enormous et therefore the memory leak is much more obvious to notice.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
chrfalchcommented, Aug 8, 2022

Sent!

0reactions
chrfalchcommented, Aug 8, 2022

(Just a small note, running on device (iOS) does not show any signs of memory leaks either.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Destroy Memory Leak — DataTables forums
I use the drawCallback function to do different customizations like setting colors and grouping on the rows, and this function is of course...
Read more >
A memory leak when capturing callb… - Apple Developer
Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your...
Read more >
java - How to avoid memory leaks in callback? - Stack Overflow
If the only references to an object are weak references, the garbage collector can reclaim the memory used by an object at any...
Read more >
Memory leak - OWASP Foundation
Memory leak on the main website for The OWASP Foundation. ... intentionally trigger a memory leak, the attacker might be able to launch...
Read more >
Finding Memory Leak in Go Service - Nylas
Here at Nylas, we have a process to diagnose memory leak for Go services. Tools such as pprof and minikube can help us...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found