Skip to content

Commit 1ab2996

Browse files
committedFeb 26, 2025
fix(cdk/tree): retainining previous objects
fixes component retaining old data in memory even when its not used causing memory usage to increase fixes #30322
1 parent 1b3c42e commit 1ab2996

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed
 

‎src/cdk/tree/tree.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,9 @@ export class CdkTree<T, K = T>
10661066
// nodes are flat, or if the tree is using a levelAccessor and the nodes are
10671067
// nested.
10681068
if (this.childrenAccessor && nodeType === 'flat') {
1069+
// clear previously generated data so we don't keep end up retaining data overtime causing
1070+
// memory leaks.
1071+
this._clearPreviousCache();
10691072
// This flattens children into a single array.
10701073
this._ariaSets.set(null, [...nodes]);
10711074
return this._flattenNestedNodesWithExpansion(nodes).pipe(
@@ -1098,6 +1101,9 @@ export class CdkTree<T, K = T>
10981101
}),
10991102
);
11001103
} else {
1104+
// clear previously generated data so we don't keep end up retaining data overtime causing
1105+
// memory leaks.
1106+
this._clearPreviousCache();
11011107
// For nested nodes, we still need to perform the node flattening in order
11021108
// to maintain our caches for various tree operations.
11031109
this._ariaSets.set(null, [...nodes]);
@@ -1125,8 +1131,9 @@ export class CdkTree<T, K = T>
11251131
return;
11261132
}
11271133

1128-
this._parents.clear();
1129-
this._ariaSets.clear();
1134+
// clear previously generated data so we don't keep end up retaining data overtime causing
1135+
// memory leaks.
1136+
this._clearPreviousCache();
11301137

11311138
for (let index = 0; index < flattenedNodes.length; index++) {
11321139
const dataNode = flattenedNodes[index];
@@ -1163,6 +1170,13 @@ export class CdkTree<T, K = T>
11631170
callback(toToggle);
11641171
}
11651172
}
1173+
1174+
/** Clears the maps we use to store parents, level & aria-sets in. */
1175+
private _clearPreviousCache() {
1176+
this._parents.clear();
1177+
this._levels.clear();
1178+
this._ariaSets.clear();
1179+
}
11661180
}
11671181

11681182
/**

0 commit comments

Comments
 (0)
Failed to load comments.