blob: 16dec5d3e18400c42c7acd9d3d8408cb00c530f5 [file] [log] [blame]
Andreas Gampecc13b222016-10-10 19:09:09 -07001/*
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
17import java.util.ArrayList;
Andreas Gampee54eee12016-10-20 19:03:58 -070018import java.util.Arrays;
Andreas Gampecc13b222016-10-10 19:09:09 -070019
20public class Main {
21 public static void main(String[] args) throws Exception {
22 System.loadLibrary(args[1]);
23
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 setupObjectFreeCallback();
32
33 enableFreeTracking(true);
34 run(l);
35
36 enableFreeTracking(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
Andreas Gampee54eee12016-10-20 19:03:58 -070046 getAndPrintTags();
Andreas Gampecc13b222016-10-10 19:09:09 -070047 System.out.println("---");
48
49 // Note: the reporting will not depend on the heap layout (which could be unstable). Walking
50 // the tag table should give us a stable output order.
51 for (int i = 10; i <= 1000; i *= 10) {
52 allocate(l, i);
53 }
54 l.clear();
55
56 Runtime.getRuntime().gc();
57
Andreas Gampee54eee12016-10-20 19:03:58 -070058 getAndPrintTags();
Andreas Gampecc13b222016-10-10 19:09:09 -070059 System.out.println("---");
60
61 Runtime.getRuntime().gc();
62
Andreas Gampee54eee12016-10-20 19:03:58 -070063 getAndPrintTags();
Andreas Gampecc13b222016-10-10 19:09:09 -070064 System.out.println("---");
65 }
66
67 private static void allocate(ArrayList<Object> l, long tag) {
68 Object obj = new Object();
69 l.add(obj);
70 setTag(obj, tag);
71 }
72
Andreas Gampee54eee12016-10-20 19:03:58 -070073 private static void getAndPrintTags() {
74 long[] freedTags = getCollectedTags();
75 Arrays.sort(freedTags);
76 System.out.println(Arrays.toString(freedTags));
77 }
78
Andreas Gampecc13b222016-10-10 19:09:09 -070079 private static native void setupObjectFreeCallback();
80 private static native void enableFreeTracking(boolean enable);
81 private static native void setTag(Object o, long tag);
Andreas Gampee54eee12016-10-20 19:03:58 -070082 private static native long[] getCollectedTags();
Andreas Gampecc13b222016-10-10 19:09:09 -070083}