Image Cache Optimization for Limited RAM Devices
Background While using branch 1.10, I encountered a memory issue when dealing with large image sets (e.g., >500 images). The current implementation of image loading in egt/src/detail/imagecache.cpp uses a cache mechanism that does not enforce any upper limit on the number of images stored. This can result in a hard fault on devices with low RAM, as memory usage grows unchecked.
Issue Unbounded Image Caching: The cache does not limit how many images can be stored, which causes memory exhaustion.
No Cache Eviction Policy: Once images are added, there is no mechanism to discard unused or least recently used images from the cache.
Solution I made the following improvements in egt/src/detail/imagecache.cpp:
Added a configurable maximum cache size (e.g., via a constant or runtime config).
Implemented a simple Least Recently Used (LRU) eviction policy to discard old images when the cache limit is reached.
Provided a function to explicitly discard images from the cache, to allow manual cache control when needed.
Key Code Changes Added a constexpr size_t kMaxCacheSize = 100; limit (can be made configurable).
Modified the image insertion logic to check the cache size and evict the least recently used image.
Introduced discard(const std::string& key) to remove specific images from the cache.
Impact These changes:
Prevent hard faults on low-RAM systems.
Improve cache reliability and performance.
Provide developers with manual control over cache memory usage.
Recommendation It is highly recommended to integrate this change in the mainline to avoid similar issues for other developers and devices with limited memory.
Please check for the reference imagecache.txt