You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/08/27 15:37:53 UTC

(commons-compress) 02/02: Use freeMemory() instead of maxMemory()

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit b075af395d674a84b0b1b7c48ab73fd736b569ce
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Wed Aug 27 11:37:46 2025 -0400

    Use freeMemory() instead of maxMemory()
---
 .../org/apache/commons/compress/MemoryLimitException.java  | 14 ++++++++------
 .../compress/archivers/zip/ZipArchiveInputStream.java      |  2 +-
 .../commons/compress/harmony/pack200/Pack200Exception.java |  2 +-
 3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/MemoryLimitException.java b/src/main/java/org/apache/commons/compress/MemoryLimitException.java
index 10c5a9d4b..9cbc659e6 100644
--- a/src/main/java/org/apache/commons/compress/MemoryLimitException.java
+++ b/src/main/java/org/apache/commons/compress/MemoryLimitException.java
@@ -27,7 +27,8 @@
  */
 public class MemoryLimitException extends CompressException {
 
-    private static final String MAXIMUM = "maximum";
+    private static final String LABEL = "free memory";
+    private static final String MAXIMUM = "free";
     private static final String BYTES = "bytes";
     private static final String KIB = "KiB";
     private static final long serialVersionUID = 1L;
@@ -59,7 +60,7 @@ private static void check(final long request, final long max, final String scale
      * @since 1.29.0
      */
     public static int checkBytes(final int request, final long max) throws MemoryLimitException {
-        check(request, max, maxMemory(), "max memory", BYTES);
+        check(request, max, newObjectMemory(), LABEL, BYTES);
         return request;
     }
 
@@ -73,7 +74,7 @@ public static int checkBytes(final int request, final long max) throws MemoryLim
      * @since 1.29.0
      */
     public static long checkBytes(final long request, final long max) throws MemoryLimitException {
-        check(request, max, maxMemory(), "max memory", BYTES);
+        check(request, max, newObjectMemory(), LABEL, BYTES);
         return request;
     }
 
@@ -87,15 +88,16 @@ public static long checkBytes(final long request, final long max) throws MemoryL
      * @since 1.29.0
      */
     public static long checkKiB(final long request, final long max) throws MemoryLimitException {
-        return check(request, max, maxMemory() / 1024, "max memory", KIB);
+        return check(request, max, newObjectMemory() / 1024, LABEL, KIB);
     }
 
-    private static long maxMemory() {
-        return Runtime.getRuntime().maxMemory();
+    private static long newObjectMemory() {
+        return Runtime.getRuntime().freeMemory();
     }
 
     /** A long instead of int to account for overflow in corrupt files. */
     private final long memoryNeededKiB;
+
     /** A long instead of int to account for overflow in corrupt files. */
     private final long memoryLimitKiB;
 
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
index 38f985116..03ad2dc79 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
@@ -378,7 +378,7 @@ private int cacheBytesRead(final ByteArrayOutputStream bos, int offset, final in
         final int cacheable = offset + lastRead - expectedDDLen - 3;
         if (cacheable > 0) {
             final int request = (bos.size() + cacheable) * 2;
-            MemoryLimitException.checkBytes(request + 2 * 1024 * 1024, Runtime.getRuntime().maxMemory()); // 2 MB headroom
+            MemoryLimitException.checkBytes(request + 2 * 1024 * 1024, Runtime.getRuntime().freeMemory()); // 2 MB headroom
             bos.write(buf.array(), 0, cacheable);
             System.arraycopy(buf.array(), cacheable, buf.array(), 0, expectedDDLen + 3);
             offset = expectedDDLen + 3;
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200Exception.java b/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200Exception.java
index f7c897adf..42505798a 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200Exception.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/Pack200Exception.java
@@ -60,7 +60,7 @@ public static int addExact(final int x, final long y) throws Pack200Exception {
      */
     public static int checkIntArray(final int size) throws Pack200Exception {
         try {
-            MemoryLimitException.checkBytes(Math.multiplyExact(Integer.BYTES, size), Runtime.getRuntime().maxMemory());
+            MemoryLimitException.checkBytes(Math.multiplyExact(Integer.BYTES, size), Runtime.getRuntime().freeMemory());
             return size;
         } catch (final MemoryLimitException | ArithmeticException e) {
             throw new Pack200Exception(e);