OSDN Git Service

am af140189: Reconcile with ics-mr1-release
[android-x86/dalvik.git] / tests / 061-out-of-memory / src / Main.java
index b5999b3..c812c81 100644 (file)
@@ -43,18 +43,13 @@ public class Main {
     private static void testOomeLarge() {
         System.out.println("testOomeLarge beginning");
 
-        /* Just shy of the typical max heap size so that it will actually
-         * try to allocate it instead of short-circuiting.
-         *
-         * TODO: stop assuming the VM defaults to 16MB max
-         */
-        final int SIXTEEN_MB = (16 * 1024 * 1024 - 32);
-
         Boolean sawEx = false;
-        byte a[];
+        byte[] a;
 
         try {
-            a = new byte[SIXTEEN_MB];
+            // Just shy of the typical max heap size so that it will actually
+            // try to allocate it instead of short-circuiting.
+            a = new byte[(int) Runtime.getRuntime().maxMemory() - 32];
         } catch (OutOfMemoryError oom) {
             //Log.i(TAG, "HeapTest/OomeLarge caught " + oom);
             sawEx = true;
@@ -71,11 +66,8 @@ public class Main {
     /* Do this in another method so that the GC has a chance of freeing the
      * list afterwards.  Even if we null out list when we're done, the conservative
      * GC may see a stale pointer to it in a register.
-     *
-     * TODO: stop assuming the VM defaults to 16MB max
      */
     private static boolean testOomeSmallInternal() {
-        final int SIXTEEN_MB = (16 * 1024 * 1024);
         final int LINK_SIZE = 6 * 4; // estimated size of a LinkedList's node
 
         LinkedList<Object> list = new LinkedList<Object>();
@@ -86,7 +78,7 @@ public class Main {
         while (objSize >= LINK_SIZE) {
             boolean sawEx = false;
             try {
-                for (int i = 0; i < SIXTEEN_MB / objSize; i++) {
+                for (int i = 0; i < Runtime.getRuntime().maxMemory() / objSize; i++) {
                     list.add((Object)new byte[objSize]);
                 }
             } catch (OutOfMemoryError oom) {