jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Ian Rogers | dd0c4b8 | 2013-01-11 09:02:55 -0800 | [diff] [blame^] | 17 | import java.lang.reflect.Method; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 18 | import java.util.ArrayList; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 19 | import java.util.List; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 20 | |
Ian Rogers | dd0c4b8 | 2013-01-11 09:02:55 -0800 | [diff] [blame^] | 21 | public class Main { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 22 | |
| 23 | public static void main(String[] args) throws Exception { |
| 24 | |
| 25 | int alloc1 = 1; |
| 26 | try { |
| 27 | List<byte[]> l = new ArrayList<byte[]>(); |
| 28 | while (true) { |
| 29 | // Allocate a MB at a time |
| 30 | l.add(new byte[1048576]); |
| 31 | alloc1++; |
| 32 | } |
| 33 | } catch (OutOfMemoryError e) { |
| 34 | } |
Ian Rogers | dd0c4b8 | 2013-01-11 09:02:55 -0800 | [diff] [blame^] | 35 | // Expand the heap to the maximum size. |
| 36 | // Reflective equivalent of: dalvik.system.VMRuntime.getRuntime().clearGrowthLimit(); |
| 37 | Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime"); |
| 38 | Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime"); |
| 39 | Object runtime = get_runtime.invoke(null); |
| 40 | Method clear_growth_limit = vm_runtime.getDeclaredMethod("clearGrowthLimit"); |
| 41 | clear_growth_limit.invoke(runtime); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 42 | |
| 43 | int alloc2 = 1; |
| 44 | try { |
| 45 | List<byte[]> l = new ArrayList<byte[]>(); |
| 46 | while (true) { |
| 47 | // Allocate a MB at a time |
| 48 | l.add(new byte[1048576]); |
| 49 | alloc2++; |
| 50 | } |
| 51 | } catch (OutOfMemoryError e2) { |
| 52 | if (alloc1 > alloc2) { |
| 53 | System.out.println("ERROR: Allocated less memory after growth" + |
| 54 | "limit cleared (" + alloc1 + " MBs > " + alloc2 + " MBs"); |
| 55 | System.exit(1); |
| 56 | } |
| 57 | } |
Ian Rogers | dd0c4b8 | 2013-01-11 09:02:55 -0800 | [diff] [blame^] | 58 | System.out.println("Test complete"); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 59 | } |
| 60 | } |