How can a growing array used as a cache with no eviction policy become a memory leak?
Short answer
If you keep pushing entries into a cache array/object without ever removing old ones, it grows unbounded for the lifetime of the app, retaining every cached value forever even if most are never accessed again — effectively a slow, self-inflicted leak.
Advertisement
AModel answer
If you keep pushing entries into a cache array/object without ever removing old ones, it grows unbounded for the lifetime of the app, retaining every cached value forever even if most are never accessed again — effectively a slow, self-inflicted leak.
const cache = [];
function cacheResult(key, value) {
cache.push({ key, value }); // never removed -> unbounded growth
}Real-world example
A long-running dashboard app whose memory usage climbs steadily because every API response ever fetched stays cached forever.
Likely follow-ups
- How would you bound this cache, e.g. with an LRU eviction strategy?
Related topics