Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 public class Main {
     18     static class ArrayMemEater {
     19         static boolean sawOome;
     20 
     21         static void blowup(char[][] holder) {
     22             try {
     23                 for (int i = 0; i < holder.length; ++i) {
     24                     holder[i] = new char[128 * 1024];
     25                 }
     26             } catch (OutOfMemoryError oome) {
     27                 ArrayMemEater.sawOome = true;
     28             }
     29         }
     30     }
     31 
     32     static class InstanceMemEater {
     33         static boolean sawOome;
     34 
     35         InstanceMemEater next;
     36         double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster.
     37 
     38         static InstanceMemEater allocate() {
     39             try {
     40                 return new InstanceMemEater();
     41             } catch (OutOfMemoryError e) {
     42                 InstanceMemEater.sawOome = true;
     43                 return null;
     44             }
     45         }
     46 
     47         static void confuseCompilerOptimization(InstanceMemEater instance) {
     48         }
     49     }
     50 
     51     static boolean triggerArrayOOM() {
     52         ArrayMemEater.blowup(new char[1 * 1024 * 1024][]);
     53         return ArrayMemEater.sawOome;
     54     }
     55 
     56     static boolean triggerInstanceOOM() {
     57         InstanceMemEater memEater = InstanceMemEater.allocate();
     58         InstanceMemEater lastMemEater = memEater;
     59         do {
     60             lastMemEater.next = InstanceMemEater.allocate();
     61             lastMemEater = lastMemEater.next;
     62         } while (lastMemEater != null);
     63         memEater.confuseCompilerOptimization(memEater);
     64         return InstanceMemEater.sawOome;
     65     }
     66 
     67     public static void main(String[] args) {
     68         if (triggerArrayOOM()) {
     69             System.out.println("NEW_ARRAY correctly threw OOME");
     70         }
     71 
     72         if (triggerInstanceOOM()) {
     73             System.out.println("NEW_INSTANCE correctly threw OOME");
     74         }
     75     }
     76 }
     77