What memory management issue might this caching implementation have?
let cache = new Map();
function process(obj) {
if (cache.has(obj)) {
return cache.get(obj);
}
const result = expensiveComputation(obj);
cache.set(obj, result);
return result;
}
This caching implementation might lead to unbounded growth: 1) The cache Map keeps growing as new unique objects are processed, 2) There's no mechanism to remove old or unused entries from the cache, 3) This can lead to memory leaks if many objects are processed over time, 4) Even if the processed objects are no longer used elsewhere, the cache still holds references to them, 5) This prevents the garbage collector from reclaiming the memory of these objects, 6) A better implementation would include a strategy to limit cache size or remove stale entries, 7) Options include using a WeakMap (if appropriate), implementing a least-recently-used (LRU) strategy, or adding explicit cache clearing, 8) Caching is a common source of memory leaks if not carefully managed with clear retention policies.