Debian Codegraph @

test/617-clinit-oome/src/Main.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 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 private static int exhaustJavaHeap(Object[] data, int index, int size) {
19 Runtime.getRuntime().gc();
20 while (size > 0) {
21 try {
22 data[index] = new byte[size];
23 index++;
24 } catch (OutOfMemoryError e) {
25 size /= 2;
26 }
27 }
28 return index;
29 }
30
31 public static void main(String[] args) {
32 Class klass = Other.class;
33 Object[] data = new Object[100000];
34 try {
35 System.out.println("Filling heap");
36
37 // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw
38 // OOME to prevent GC thrashing, even if later allocations may succeed.
39 Runtime.getRuntime().gc();
40 System.runFinalization();
41 // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here.
42
43 int index = 0;
44 int initial_size = 256 * 1024 * 1024;
45 // Repeat to ensure there is no space left on the heap.
46 index = exhaustJavaHeap(data, index, initial_size);
47 index = exhaustJavaHeap(data, index, /*size*/ 4);
48 index = exhaustJavaHeap(data, index, /*size*/ 4);
49
50 // Initialize now that the heap is full.
51 Other.print();
52 } catch (OutOfMemoryError e) {
53 } catch (Exception e) {
54 System.out.println(e);
55 }
56 }
57 }