Test908.java revision a8380240c8c9752c8b43926f677adcac11c2f52f
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
17package art;
18
19import java.util.ArrayList;
20
21public class Test908 {
22  public static void run() throws Exception {
23    Main.bindAgentJNIForClass(Test908.class);
24    doTest();
25  }
26
27  public static void doTest() throws Exception {
28    // Use a list to ensure objects must be allocated.
29    ArrayList<Object> l = new ArrayList<>(100);
30
31    setupGcCallback();
32
33    enableGcTracking(true);
34    run(l);
35
36    enableGcTracking(false);
37    run(l);
38  }
39
40  private static void run(ArrayList<Object> l) {
41    allocate(l, 1);
42    l.clear();
43
44    Runtime.getRuntime().gc();
45
46    printStats();
47
48    // Note: the reporting will not depend on the heap layout (which could be unstable). Walking
49    //       the tag table should give us a stable output order.
50    for (int i = 10; i <= 1000; i *= 10) {
51      allocate(l, i);
52    }
53    l.clear();
54
55    Runtime.getRuntime().gc();
56
57    printStats();
58
59    Runtime.getRuntime().gc();
60
61    printStats();
62  }
63
64  private static void allocate(ArrayList<Object> l, long tag) {
65    Object obj = new Object();
66    l.add(obj);
67  }
68
69  private static void printStats() {
70      System.out.println("---");
71      int s = getGcStarts();
72      int f = getGcFinishes();
73      System.out.println((s > 0) + " " + (f > 0));
74  }
75
76  private static native void setupGcCallback();
77  private static native void enableGcTracking(boolean enable);
78  private static native int getGcStarts();
79  private static native int getGcFinishes();
80}
81