8078582: java/lang/Runtime/exec/LotsOfOutput.java fails intermittently with Process consumes memory
authorrriggs
Wed, 20 May 2015 11:17:09 -0400
changeset 30683 a69fe0acc0ab
parent 30682 a6f608b9b14d
child 30684 ac2a15333983
8078582: java/lang/Runtime/exec/LotsOfOutput.java fails intermittently with Process consumes memory Summary: Small allocations may bump total memory, raise THRESHOLD to 2Mb Reviewed-by: martin
jdk/test/java/lang/Runtime/exec/LotsOfOutput.java
--- a/jdk/test/java/lang/Runtime/exec/LotsOfOutput.java	Wed May 20 17:16:58 2015 +0300
+++ b/jdk/test/java/lang/Runtime/exec/LotsOfOutput.java	Wed May 20 11:17:09 2015 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,12 +23,22 @@
 
 /**
  * @test
- * @bug 4369826
+ * @bug 4369826 8078582
  * @summary Process with lots of output should not crash VM
+ * @key intermittent
  * @author kladko
  */
 
 public class LotsOfOutput {
+    static final Runtime runtime = Runtime.getRuntime();
+
+    // Allow memory to grow by up to 1Mb total
+    static final int THRESHOLD = 1048576;
+
+    // Compute used memory
+    static long usedMemory() {
+        return runtime.totalMemory() - runtime.freeMemory();
+    }
 
     public static void main(String[] args) throws Exception {
         if (! UnixCommands.isUnix) {
@@ -37,18 +47,21 @@
         }
         UnixCommands.ensureCommandsAvailable("cat");
 
-        Process p = Runtime.getRuntime().exec(UnixCommands.cat() + " /dev/zero");
-        long initMemory = Runtime.getRuntime().totalMemory();
-        for (int i=1; i< 10; i++) {
+        Process p = runtime.exec(UnixCommands.cat() + " /dev/zero");
+        long initMemory = usedMemory();
+        boolean growing = false;
+        for (int i = 1; i < 10; i++) {
             Thread.sleep(100);
-            long totalMemory = Runtime.getRuntime().totalMemory();
-            if (totalMemory != initMemory) {
-                System.out.printf("consuming memory: i: %d, initial: %d, total: %d, delta: %d%n",
-                        i, initMemory, totalMemory, totalMemory - initMemory);
+            long used = usedMemory();
+            if (used != initMemory) {
+                System.out.printf("consuming memory: i: %d, initial: %d, used: %d, delta: %d%n",
+                        i, initMemory, used, used - initMemory);
             }
-            if (totalMemory > initMemory + 1000000)
-                throw new Exception("Process consumes memory.");
+            if (used > initMemory + THRESHOLD)
+                growing = true;
         }
+        if (growing)
+            throw new Exception("Process consumes memory.");
 
     }
 }