Potential memory leak
See original GitHub issueI 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:
- Over time, memory must build up because there’s no way to clear the cache.
- 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:
- Created a year ago
- Comments:13 (9 by maintainers)
Top GitHub Comments
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.
@wwwtyro Thank you! Indeed, I forgot to check the parent class of
Scissor
.Good idea on keeping the y-axis!