ahrm / sioyek

Sioyek is a PDF viewer with a focus on textbooks and research papers
https://sioyek.info/
GNU General Public License v3.0
7.09k stars 235 forks source link

Low memory mode #954

Open char101 opened 8 months ago

char101 commented 8 months ago

Hi,

Mupdf tools supports a low memory flag.

The default mupdf cache is 256MB, this means opening 4 documents will use up to 1GB memory.

In my test opening 4 pdfs:

So without cache the working set memory is kept low, what eats up memory is the GPU memory. After the GPU memory > 1GB, the application hang.

It would be useful if sioyek can also support a low memory config and even better is if the GPU memory can be also made constant.

diff --git a/pdf_viewer/main.cpp b/pdf_viewer/main.cpp
index 9169b6d..297a490 100644
--- a/pdf_viewer/main.cpp
+++ b/pdf_viewer/main.cpp
@@ -1059,7 +1059,7 @@ int main(int argc, char* args[]) {
     locks.lock = lock_mutex;
     locks.unlock = unlock_mutex;

-    fz_context* mupdf_context = fz_new_context(nullptr, &locks, FZ_STORE_DEFAULT);
+    fz_context* mupdf_context = fz_new_context(nullptr, &locks, 1);

     if (!VERBOSE) {
         fz_set_warning_callback(mupdf_context, nullptr, nullptr);
diff --git a/pdf_viewer/pdf_renderer.cpp b/pdf_viewer/pdf_renderer.cpp
index 62c82a9..1fcedf5 100644
--- a/pdf_viewer/pdf_renderer.cpp
+++ b/pdf_viewer/pdf_renderer.cpp
@@ -582,6 +582,7 @@ void PdfRenderer::run(int thread_index) {

                     fz_clear_pixmap_with_value(mupdf_context, rendered_pixmap, 0xFF);
                     fz_device* draw_device = fz_new_draw_device(mupdf_context, transform_matrix, rendered_pixmap);
+                    fz_enable_device_hints(mupdf_context, draw_device, FZ_NO_CACHE);

                     if (req.should_render_annotations) {
                         fz_run_page(mupdf_context, page, draw_device, fz_identity, nullptr); // todo: use cookie to report progress
ahrm commented 8 months ago

The GPU memory already is constant, the constant just might be higher than your machine can handle. We have added a num_cached_pages config option recently, you can set it to a lower value to reduce memory usage. Also what is your display resolution?

char101 commented 8 months ago

I have dual monitor, the app is running in a QHD monitor. The other monitor is FHD. The application window itself is small, probably around 800 x 1000 pixels.

Yeah I'm not sure about the GPU memory. I'm using System Informer to check the memory usage and it is showing that firefox uses 32 GB dedicated GPU bytes, while the GPU only has 2 GB memory.

char101 commented 8 months ago

I tested adding num_cached_pages 1 to the configuration and can't see much difference with the memory or GPU memory.

1 document open 1doc

4 documents open 2024-01-29 16_06_20-System Informer  LTSC_char

This is with my patch above. As can be seen in the screenshots, the working set memory does not increase much, but the private bytes increases a lot.

char101 commented 8 months ago

So far with no cache, the drawback that I experienced is flickering when the page is being rerendered. Currently I am using a 16 MB cache value and hasn't experienced any flickering.