Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/LotsOfOutput.java
41153 views
/*1* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4369826 8078582 819088426* @run main/othervm LotsOfOutput27* @summary Process with lots of output should not crash VM28* @author kladko29*/3031public class LotsOfOutput {32static final Runtime runtime = Runtime.getRuntime();3334// Allow memory to grow by up to 1Mb total35static final int THRESHOLD = 1048576;3637// Compute used memory38static long usedMemory() {39return runtime.totalMemory() - runtime.freeMemory();40}4142public static void main(String[] args) throws Exception {43if (! UnixCommands.isUnix) {44System.out.println("For UNIX only");45return;46}47UnixCommands.ensureCommandsAvailable("cat");4849Process p = runtime.exec(UnixCommands.cat() + " /dev/zero");50long prev = usedMemory();51int growing = 0;52for (int i = 1; i < 10; i++) {53Thread.sleep(100);54long used = usedMemory();55if (used != prev) {56System.out.printf("consuming memory: i: %d, prev: %d, used: %d, delta: %d%n",57i, prev, used, used - prev);58}59if (used > prev + THRESHOLD)60growing += 1;61prev = used;62}63if (growing > 2)64throw new Exception("Process consumes memory: growing " + growing);6566}67}686970