From d5156cbe538d0760109e148fcea5815f0d05803c Mon Sep 17 00:00:00 2001 From: gcd Date: Thu, 1 Oct 2026 14:17:56 +0200 Subject: [PATCH] fix(android): skip redundant document reloads in drawPdf() to fix a native memory leak drawPdf() runs several times per mount (setPath, onAfterUpdateTransaction, onAttachedToWindow) and again on every prop transaction, and each call reloads the document. A reload cancels the in-flight DecodingAsyncTask, but AndroidPdfViewer 4.0.1's DecodingAsyncTask.onCancelled() never disposes the document it may already have opened, which leaked about 3 MB of native memory per mount. drawPdf() now skips the reload when the view is not recycled and the load configuration is unchanged. page is left out of that configuration since it tracks the displayed page and setPage() already applies it to the live view. Co-Authored-By: Claude Opus 5.5 --- .../src/main/java/org/wonday/pdf/PdfView.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/android/src/main/java/org/wonday/pdf/PdfView.java b/android/src/main/java/org/wonday/pdf/PdfView.java index e6dab0cb..101f46f8 100644 --- a/android/src/main/java/org/wonday/pdf/PdfView.java +++ b/android/src/main/java/org/wonday/pdf/PdfView.java @@ -59,6 +59,7 @@ import java.io.FileNotFoundException; import java.io.InputStream; +import java.util.Arrays; import com.google.gson.Gson; @@ -99,6 +100,9 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl private float restoreXOffset = 0; private float restoreYOffset = 0; + // Configuration of the last document load started by drawPdf(), see loadConfig(). + private String lastLoadConfig = null; + // used to store the parameters for `super.onSizeChanged` private int oldW = 0; private int oldH = 0; @@ -344,6 +348,16 @@ public void drawPdf() { if (this.path != null){ + // drawPdf() runs several times per mount (setPath, onAfterUpdateTransaction, + // onAttachedToWindow) and on every prop transaction. Each reload cancels the + // in-flight DecodingAsyncTask, which never closes the native document it may + // already have opened, so skip reloads that would not change anything. + String loadConfig = this.loadConfig(); + if (!this.isRecycled() && loadConfig.equals(this.lastLoadConfig)) { + return; + } + this.lastLoadConfig = loadConfig; + // set scale this.setMinZoom(this.minScale); this.setMaxZoom(this.maxScale); @@ -414,6 +428,16 @@ public void drawPdf() { } } + // Every prop drawPdf() passes to the Configurator. page is left out: it tracks the + // displayed page and setPage() applies it to the live view. + private String loadConfig() { + return Arrays.asList(this.path, this.horizontal, this.spacing, this.password, + this.enableAntialiasing, this.fitPolicy, this.pageSnap, this.autoSpacing, + this.pageFling, this.singlePage, this.scrollEnabled, this.enableDoubleTapZoom, + this.enableAnnotationRendering, this.enableRTL, this.minScale, + this.maxScale).toString(); + } + public void setEnableDoubleTapZoom(boolean enableDoubleTapZoom) { this.enableDoubleTapZoom = enableDoubleTapZoom; }