Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 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 | import java.util.ArrayList; |
| 18 | |
| 19 | public class Main { |
| 20 | public static void main(String[] args) throws Exception { |
| 21 | System.loadLibrary(args[1]); |
| 22 | |
| 23 | doTest(); |
| 24 | } |
| 25 | |
| 26 | public static void doTest() throws Exception { |
| 27 | // Use a list to ensure objects must be allocated. |
| 28 | ArrayList<Object> l = new ArrayList<>(100); |
| 29 | |
| 30 | setupObjectFreeCallback(); |
| 31 | |
| 32 | enableFreeTracking(true); |
| 33 | run(l); |
| 34 | |
| 35 | enableFreeTracking(false); |
| 36 | run(l); |
| 37 | } |
| 38 | |
| 39 | private static void run(ArrayList<Object> l) { |
| 40 | allocate(l, 1); |
| 41 | l.clear(); |
| 42 | |
| 43 | Runtime.getRuntime().gc(); |
| 44 | |
| 45 | System.out.println("---"); |
| 46 | |
| 47 | // Note: the reporting will not depend on the heap layout (which could be unstable). Walking |
| 48 | // the tag table should give us a stable output order. |
| 49 | for (int i = 10; i <= 1000; i *= 10) { |
| 50 | allocate(l, i); |
| 51 | } |
| 52 | l.clear(); |
| 53 | |
| 54 | Runtime.getRuntime().gc(); |
| 55 | |
| 56 | System.out.println("---"); |
| 57 | |
| 58 | Runtime.getRuntime().gc(); |
| 59 | |
| 60 | System.out.println("---"); |
| 61 | } |
| 62 | |
| 63 | private static void allocate(ArrayList<Object> l, long tag) { |
| 64 | Object obj = new Object(); |
| 65 | l.add(obj); |
| 66 | setTag(obj, tag); |
| 67 | } |
| 68 | |
| 69 | private static native void setupObjectFreeCallback(); |
| 70 | private static native void enableFreeTracking(boolean enable); |
| 71 | private static native void setTag(Object o, long tag); |
| 72 | } |