Hand-Rolled LRU Caches Can Leak Memory When Sized by Entry Count
A common LRU cache implementation limits capacity by number of entries, which ignores the real constraint of memory for items like decoded image thumbnails. The article recommends tracking a running byte budget and evicting from the least-recently-used end in a while loop, so a single large insert can free enough space. It also points out that using an ArrayDeque with remove(key) makes each access O(n), which can cause performance problems as the cache grows. The suggested fix pairs a hash map with a doubly linked list so that get and put run in constant time. The author notes these issues often surface only later as jank, so catching them early is the key skill.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.



Discussion (0)
Log in to join the discussion and vote.
Log in