1/* 2 * Copyright 2009 Google Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.util.*; 25import java.io.*; 26 27/** 28 * A manual test that demonstrates the ability to start a subprocess 29 * on Linux without getting ENOMEM. Run this test like: 30 * 31 * java -Xmx7000m BigFork 32 * 33 * providing a -Xmx flag suitable for your operating environment. 34 * Here's the bad old behavior: 35 * 36 * ==> java -Xmx7000m -esa -ea BigFork 37 * ------- 38 * CommitLimit: 6214700 kB 39 * Committed_AS: 2484452 kB 40 * ------- 41 * size=4.6GB 42 * ------- 43 * CommitLimit: 6214700 kB 44 * Committed_AS: 7219680 kB 45 * ------- 46 * Exception in thread "main" java.io.IOException: Cannot run program "/bin/true": java.io.IOException: error=12, Cannot allocate memory 47 * at java.lang.ProcessBuilder.start(ProcessBuilder.java:1018) 48 * at BigFork.main(BigFork.java:79) 49 * Caused by: java.io.IOException: java.io.IOException: error=12, Cannot allocate memory 50 * at java.lang.UNIXProcess.<init>(UNIXProcess.java:190) 51 * at java.lang.ProcessImpl.start(ProcessImpl.java:128) 52 * at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010) 53 * ... 1 more 54 */ 55public class BigFork { 56 static final Random rnd = new Random(); 57 static void touchPages(byte[] chunk) { 58 final int pageSize = 4096; 59 for (int i = 0; i < chunk.length; i+= pageSize) { 60 chunk[i] = (byte) rnd.nextInt(); 61 } 62 } 63 64 static void showCommittedMemory() throws IOException { 65 BufferedReader r = 66 new BufferedReader( 67 new InputStreamReader( 68 new FileInputStream("/proc/meminfo"))); 69 System.out.println("-------"); 70 String line; 71 while ((line = r.readLine()) != null) { 72 if (line.startsWith("Commit")) { 73 System.out.printf("%s%n", line); 74 } 75 } 76 System.out.println("-------"); 77 } 78 79 public static void main(String[] args) throws Throwable { 80 showCommittedMemory(); 81 82 final int chunkSize = 1024 * 1024 * 100; 83 List<byte[]> chunks = new ArrayList<byte[]>(100); 84 try { 85 for (;;) { 86 byte[] chunk = new byte[chunkSize]; 87 touchPages(chunk); 88 chunks.add(chunk); 89 } 90 } catch (OutOfMemoryError e) { 91 chunks.set(0, null); // Free up one chunk 92 System.gc(); 93 int size = chunks.size(); 94 System.out.printf("size=%.2gGB%n", (double)size/10); 95 96 showCommittedMemory(); 97 98 // Can we fork/exec in our current bloated state? 99 Process p = new ProcessBuilder("/bin/true").start(); 100 p.waitFor(); 101 } 102 } 103} 104