Skip to content

Commit

Permalink
fix: memory leak caused by function cache
Browse files Browse the repository at this point in the history
  • Loading branch information
wang1212 committed Jan 17, 2025
1 parent bc84b68 commit bab64ee
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-birds-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@antv/g-lite': patch
---

fix: memory leak caused by function cache
3 changes: 3 additions & 0 deletions packages/g-lite/src/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
isBrowser,
isInFragment,
raf,
memoize,
} from './utils';

export function isCanvas(value: any): value is Canvas {
Expand Down Expand Up @@ -352,6 +353,8 @@ export class Canvas extends EventTarget implements ICanvas {
* @param skipTriggerEvent - whether to skip trigger destroy event
*/
destroy(cleanUp = true, skipTriggerEvent?: boolean) {
memoize.clearCache();

if (!skipTriggerEvent) {
this.dispatchEvent(new CustomEvent(CanvasEvent.BEFORE_DESTROY));
}
Expand Down
7 changes: 7 additions & 0 deletions packages/g-lite/src/utils/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ export function memoize<F = (...args: unknown[]) => unknown>(
return result;
};
memoized.cache = new (memoize.Cache || Map)();
memoize.cacheList.push(memoized.cache);

return memoized as F;
}

memoize.Cache = Map;
memoize.cacheList = [];
memoize.clearCache = () => {
memoize.cacheList.forEach((cache) =>
(cache as unknown as Map<unknown, unknown>).clear(),
);
};

0 comments on commit bab64ee

Please sign in to comment.