Path: blob/master/test/jdk/java/lang/ProcessBuilder/BigFork.java
41149 views
/*1* Copyright 2009 Google Inc. 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*/2223import java.util.*;24import java.io.*;2526/**27* A manual test that demonstrates the ability to start a subprocess28* on Linux without getting ENOMEM. Run this test like:29*30* java -Xmx7000m BigFork31*32* providing a -Xmx flag suitable for your operating environment.33* Here's the bad old behavior:34*35* ==> java -Xmx7000m -esa -ea BigFork36* -------37* CommitLimit: 6214700 kB38* Committed_AS: 2484452 kB39* -------40* size=4.6GB41* -------42* CommitLimit: 6214700 kB43* Committed_AS: 7219680 kB44* -------45* Exception in thread "main" java.io.IOException: Cannot run program "/bin/true": java.io.IOException: error=12, Cannot allocate memory46* at java.lang.ProcessBuilder.start(ProcessBuilder.java:1018)47* at BigFork.main(BigFork.java:79)48* Caused by: java.io.IOException: java.io.IOException: error=12, Cannot allocate memory49* at java.lang.UNIXProcess.<init>(UNIXProcess.java:190)50* at java.lang.ProcessImpl.start(ProcessImpl.java:128)51* at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)52* ... 1 more53*/54public class BigFork {55static final Random rnd = new Random();56static void touchPages(byte[] chunk) {57final int pageSize = 4096;58for (int i = 0; i < chunk.length; i+= pageSize) {59chunk[i] = (byte) rnd.nextInt();60}61}6263static void showCommittedMemory() throws IOException {64BufferedReader r =65new BufferedReader(66new InputStreamReader(67new FileInputStream("/proc/meminfo")));68System.out.println("-------");69String line;70while ((line = r.readLine()) != null) {71if (line.startsWith("Commit")) {72System.out.printf("%s%n", line);73}74}75System.out.println("-------");76}7778public static void main(String[] args) throws Throwable {79showCommittedMemory();8081final int chunkSize = 1024 * 1024 * 100;82List<byte[]> chunks = new ArrayList<byte[]>(100);83try {84for (;;) {85byte[] chunk = new byte[chunkSize];86touchPages(chunk);87chunks.add(chunk);88}89} catch (OutOfMemoryError e) {90chunks.set(0, null); // Free up one chunk91System.gc();92int size = chunks.size();93System.out.printf("size=%.2gGB%n", (double)size/10);9495showCommittedMemory();9697// Can we fork/exec in our current bloated state?98Process p = new ProcessBuilder("/bin/true").start();99p.waitFor();100}101}102}103104105