How to Clear Image Cache in PDF.js Worker from Main Thread in a Custom Build? (Memory Leak Issue) #20315
Unanswered
Avinashkr1229
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi PDF.js community,
I’m using a custom build of PDF.js in an Angular application. I want to clear the image cache (GlobalImageCache) after rendering a PDF, but I’ve run into a problem:
The image cache is actually created and used inside the PDF.js web worker, not in the main thread.
Calling clearGlobalImageCache() in the main thread only clears the main thread’s (unused) cache, not the worker’s.
I want to trigger cache clearing in the worker from the main thread, but there’s no built-in API for this.
What I’ve tried:
I exposed a clearGlobalImageCache method in my custom build and can call it from the main thread.
I attempted to use postMessage to send a custom message to the worker, and added a handler in the worker to call [clearGlobalImageCache] { type: 'CLEAR_GLOBAL_IMAGE_CACHE' }.
Problem/Note:
Because of this, system memory usage doubles every time I open the dialog to view 50 records with 180 pages each. After the 5th iteration, the app consumes all available system memory and starts flickering. Only after about 10 minutes does the memory begin to release.
This makes the app unusable for large or repeated PDF viewing sessions.
Questions:
Is there an official or recommended way to clear the image cache inside the PDF.js worker from the main thread?
Is my approach of adding a custom message handler in the worker safe and compatible with PDF.js’s architecture?
Are there any pitfalls or best practices for managing cache or memory in custom PDF.js builds with workers?
Any advice or examples would be greatly appreciated!
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions