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; |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 18 | import java.util.Arrays; |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 19 | |
| 20 | public class Main { |
| 21 | public static void main(String[] args) throws Exception { |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 22 | doTest(); |
| 23 | } |
| 24 | |
| 25 | public static void doTest() throws Exception { |
| 26 | // Use a list to ensure objects must be allocated. |
| 27 | ArrayList<Object> l = new ArrayList<>(100); |
| 28 | |
| 29 | setupObjectFreeCallback(); |
| 30 | |
| 31 | enableFreeTracking(true); |
| 32 | run(l); |
| 33 | |
| 34 | enableFreeTracking(false); |
| 35 | run(l); |
| 36 | } |
| 37 | |
| 38 | private static void run(ArrayList<Object> l) { |
| 39 | allocate(l, 1); |
| 40 | l.clear(); |
| 41 | |
| 42 | Runtime.getRuntime().gc(); |
| 43 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 44 | getAndPrintTags(); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 56 | getAndPrintTags(); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 57 | System.out.println("---"); |
| 58 | |
| 59 | Runtime.getRuntime().gc(); |
| 60 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 61 | getAndPrintTags(); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 62 | System.out.println("---"); |
| 63 | } |
| 64 | |
| 65 | private static void allocate(ArrayList<Object> l, long tag) { |
| 66 | Object obj = new Object(); |
| 67 | l.add(obj); |
| 68 | setTag(obj, tag); |
| 69 | } |
| 70 | |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 71 | private static void getAndPrintTags() { |
| 72 | long[] freedTags = getCollectedTags(); |
| 73 | Arrays.sort(freedTags); |
| 74 | System.out.println(Arrays.toString(freedTags)); |
| 75 | } |
| 76 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 77 | private static native void setupObjectFreeCallback(); |
| 78 | private static native void enableFreeTracking(boolean enable); |
| 79 | private static native void setTag(Object o, long tag); |
Andreas Gampe | e54eee1 | 2016-10-20 19:03:58 -0700 | [diff] [blame] | 80 | private static native long[] getCollectedTags(); |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 81 | } |