-
Notifications
You must be signed in to change notification settings - Fork 687
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
Allow to disable the images cache to reduce the memory footprint #1153
Comments
We could also configure a max cache size and/or a threshold to store an image in the cache if the size of the image is lower than X bytes. Let me know what you think. |
I ran into a somewhat related problem, where adding the same image multiple times was reading that file from disk every time in order to hash its content and determine if it was already added. My way of solving the problem was to add an explicit https://gist.github.com/Fustrate/cf1b3ce8b227e385287963c23edb8c72#file-prawn-images-rb My use case is Rails-related, so I put this in an initializer, but it's framework agnostic. Note that this won't work out of the box with images inside groups from the If you still have this problem, and you're only interested in skipping the cache, you only need the |
Thanks for sharing |
Currently, Prawn is using an image registry to "cache" images:
prawn/lib/prawn/images.rb
Lines 84 to 107 in d980247
Here's the memory consumption for a single 321,2 ko PNG image (2144x784 pixels) using the following Ruby code:
With cache
Without cache
As you can see the retained memory goes from 150102211 bytes (150 Mbytes) to 10019166 bytes (10 Mbytes).
On a large PDF document (300 pages with 60 images) the memory consumption goes from more than 8Gb to 5Gb.
Surprisingly, the process is also faster! I don't know the exact reason but it might be related to the fact that we do less work (ie. we do not compute a hexdigest for every image) and that there's less memory pressure.
I guess it makes sense to use a cache when an image is used on every page (for instance, when using a logo on the header or footer) but in practice it can do more harm than good.
Maybe we should add an option to decide if we want to cache the image or not? And/or a global flag to enable/disable the cache?
The text was updated successfully, but these errors were encountered: