Skip to content

Image Cache Optimization for Limited RAM Devices #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mkumar7404508129 opened this issue May 14, 2025 · 3 comments
Open

Image Cache Optimization for Limited RAM Devices #34

mkumar7404508129 opened this issue May 14, 2025 · 3 comments

Comments

@mkumar7404508129
Copy link

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

@gbmbg
Copy link

gbmbg commented May 22, 2025

Good morning
Did you face something like us (#33) before using your improvement?
And if yes, did it solve your problem? Is your RAM usage stable now?
Best Regards

@mkumar7404508129
Copy link
Author

Previously, my app was crashing due to memory issues—specifically from excessive caching. The crash used to happen within 2 minutes of continuous use and consistently triggered the same error you referenced (#33).

After optimising the image buffer to hold only 5 images at a time, the issue has been resolved. I tested the app continuously for 5 hours after this change, and it has been running smoothly without any crashes.

@gbmbg
Copy link

gbmbg commented May 26, 2025

Great thanks a lot!
May I ask you to check kmalloc64 with and without your application running?
Could you please tell me which kernel are you using and on which chip?
Best Regards

P.S.
I checked kmalloc64 with this, and in our case it costantly increases:
watch -n 2 cat /proc/slabinfo | grep kmalloc-64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants