Merge from mainline.
[official-gcc.git] / libjava / classpath / testsuite / java.lang / OutOfMemoryErrorTest.java
blob2dd6b15800f2a28fe723bce3092d14282c7c1586
1 import java.util.Vector;
3 /**
4 * Under JavaSoft's VM they arbitarily limit the amount of memory
5 * a Java application can use (though this can be overridden). The
6 * point here is to check to see whether or not an application being
7 * run by Japhar will ever get the OutOfMemoryError or not when resources
8 * are scarce. --brian
9 */
10 public class OutOfMemoryErrorTest
12 public static void main(String[] argv)
14 Vector v = null;
15 Runtime r = null;
16 long free = 0, total = 0;
17 // quickly approach memory limit 1M at a time
18 try {
19 r = Runtime.getRuntime();
20 v = new Vector();
21 while(true)
23 v.addElement(new byte[1048576]);
26 // out of memory error
27 catch (OutOfMemoryError oomerr1)
29 // slowly encroach on memory limit 2 bytes+ at a time
30 try {
31 while(true)
33 v.addElement(new byte[2]);
36 // out of memory error
37 catch (OutOfMemoryError oomerr2)
39 if (r != null)
41 free = r.freeMemory();
42 total = r.totalMemory();
43 v = null;
44 r.gc();
45 // System.out.println("free = " + free);
46 // System.out.println("total = " + total);
47 System.out.println("PASSED: ");
49 else
50 System.out.println("FAILED: runtime unknown");
53 // generic error
54 catch (Error err)
56 System.out.println("FAILED: unexpected error");
58 // generic exception
59 catch (Exception e)
61 System.out.println("FAILED: unexpected exception");