diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java index b00c6f037f0..41ef23e20ff 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java @@ -1264,6 +1264,7 @@ public void close() throws IOException if (signingSupport != null) { firstException = IOUtils.closeAndLogException(signingSupport, LOG, "SigningSupport", firstException); + signingSupport = null; } // close all intermediate I/O streams @@ -1281,6 +1282,16 @@ public void close() throws IOException firstException = IOUtils.closeAndLogException(ttf, LOG, "TrueTypeFont", firstException); } + // Release large data structures immediately so the GC can reclaim memory + // even if callers hold a reference to this COSDocument instance. + fontsToSubset.clear(); + fontsToClose.clear(); + documentInformation = null; + documentCatalog = null; + encryption = null; + resourceCache = null; + accessPermission = null; + // rethrow first exception to keep method contract if (firstException != null) {