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.

Potential memory leak

See original GitHub issue

I am using CandyGraph to render a large number of plots per view (50-100) with multiples views. I noticed that every time I switch the view back and forth, the memory footprint of my page goes up and up.

After using the memory profiler of Chrome, I found out that 72% of the memory was retained by coordinateScopeCache. It turns out that CandyGraph is caching a Regl DrawCommand for every unique coordinate system in getCoordinateScope:

  private getCoordinateScope(coords: CoordinateSystem) {
    let scope = this.coordinateScopeCache.get(coords);
    if (!scope) {
      scope = coords.scope(this.regl);
      this.coordinateScopeCache.set(coords, scope);
    }
    return scope;
  }

https://github.com/wwwtyro/candygraph/blob/master/src/candygraph.ts#L182-L189

Unless I miss something it looks like this cache is never cleared nor is it clearable by a candygraph user. (Something similar seems to be going on for getCompositeScope().)

This seems to be bad for two reasons:

  1. Over time, memory must build up because there’s no way to clear the cache.
  2. It’s hard not to slow down the memory build up because coords are created every time a plot is created.

I would propose a function similar to clearPositionBuffers that grants access to the internal coordinateScopeCache and compositeScopeCache. This would allow the user to periodically clear both caches entirely or clear caches upon destruction of a plot (e.g., when I navigate to a new view, I know that I will never re-use the previous coordinate instance again, so I can remove it from the cache)

Actually, why not simply making coordinateScopeCache and compositeScopeCache public? There’s no harm in clearing the cache or is there?

Update: I guess similarly, the commandCache is dangerous as it’s not clearable.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
flekschascommented, May 7, 2022

I agree! Exposing the internals is not a good idea. I actually started working on a solution on Friday and seems to work. I’ll post more details after the weekend but my idea was to automatically remove cached objects/functions when they are disposed because it seems like there would never be a case where one would run a draw command on disposed resources.

0reactions
shamrincommented, Jul 8, 2022

@wwwtyro Thank you! Indeed, I forgot to check the parent class of Scissor.

Good idea on keeping the y-axis!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Potential memory leak? - Stack Overflow
There is no memory leak in your case. Memory leak happens when you allocate memory from head and not freeing after ...
Read more >
What is Memory Leak? How can we avoid? - GeeksforGeeks
The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.
Read more >
Memory leak - OWASP Foundation
A memory leak is an unintentional form of memory consumption whereby the developer fails to free an allocated block of memory when no...
Read more >
Understanding Memory Leaks in Java - Baeldung
The first scenario that can cause a potential memory leak is heavy use of static variables. In Java, static fields have a life...
Read more >
Memory leak detection - How to find, eliminate, and avoid
A memory leak is any portion of an application which uses memory without eventually freeing it. By memory, we're talking about RAM, not ......
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