| Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 dalvik.system.VMDebug; |
| 18 | import java.io.IOException; |
| Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 19 | import java.lang.ref.PhantomReference; |
| 20 | import java.lang.ref.ReferenceQueue; |
| 21 | import java.lang.ref.WeakReference; |
| Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 22 | import libcore.util.NativeAllocationRegistry; |
| Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Program used to create a heap dump for test purposes. |
| 26 | */ |
| 27 | public class Main { |
| 28 | // Keep a reference to the DumpedStuff instance so that it is not garbage |
| 29 | // collected before we take the heap dump. |
| 30 | public static DumpedStuff stuff; |
| 31 | |
| Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 32 | public static class ObjectTree { |
| 33 | public ObjectTree left; |
| 34 | public ObjectTree right; |
| 35 | |
| 36 | public ObjectTree(ObjectTree left, ObjectTree right) { |
| 37 | this.left = left; |
| 38 | this.right = right; |
| 39 | } |
| 40 | } |
| 41 | |
| Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 42 | // We will take a heap dump that includes a single instance of this |
| 43 | // DumpedStuff class. Objects stored as fields in this class can be easily |
| 44 | // found in the hprof dump by searching for the instance of the DumpedStuff |
| 45 | // class and reading the desired field. |
| 46 | public static class DumpedStuff { |
| 47 | public String basicString = "hello, world"; |
| Richard Uhler | c7f7712 | 2016-02-11 08:48:24 -0800 | [diff] [blame] | 48 | public char[] charArray = "char thing".toCharArray(); |
| Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 49 | public String nullString = null; |
| 50 | public Object anObject = new Object(); |
| Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 51 | public ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>(); |
| 52 | public PhantomReference aPhantomReference = new PhantomReference(anObject, referenceQueue); |
| 53 | public WeakReference aWeakReference = new WeakReference(anObject, referenceQueue); |
| Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 54 | public byte[] bigArray; |
| Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 55 | public ObjectTree[] gcPathArray = new ObjectTree[]{null, null, |
| 56 | new ObjectTree( |
| 57 | new ObjectTree(null, new ObjectTree(null, null)), |
| 58 | new ObjectTree(null, null)), |
| 59 | null}; |
| Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 60 | |
| 61 | DumpedStuff() { |
| 62 | int N = 1000000; |
| 63 | bigArray = new byte[N]; |
| 64 | for (int i = 0; i < N; i++) { |
| 65 | bigArray[i] = (byte)((i*i) & 0xFF); |
| 66 | } |
| Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 67 | |
| Richard Uhler | 7d3c739 | 2016-05-03 13:05:50 -0700 | [diff] [blame] | 68 | NativeAllocationRegistry registry = new NativeAllocationRegistry( |
| 69 | Main.class.getClassLoader(), 0x12345, 42); |
| Richard Uhler | 1a5baaa | 2015-12-21 12:47:26 -0800 | [diff] [blame] | 70 | registry.registerNativeAllocation(anObject, 0xABCDABCD); |
| Richard Uhler | 6928649 | 2016-09-20 10:41:47 +0100 | [diff] [blame] | 71 | |
| 72 | gcPathArray[2].right.left = gcPathArray[2].left.right; |
| Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 73 | } |
| Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public static void main(String[] args) throws IOException { |
| 77 | if (args.length < 1) { |
| 78 | System.err.println("no output file specified"); |
| 79 | return; |
| 80 | } |
| 81 | String file = args[0]; |
| 82 | |
| 83 | // Allocate the instance of DumpedStuff. |
| 84 | stuff = new DumpedStuff(); |
| 85 | |
| 86 | // Take a heap dump that will include that instance of DumpedStuff. |
| 87 | System.err.println("Dumping hprof data to " + file); |
| 88 | VMDebug.dumpHprofData(file); |
| 89 | } |
| 90 | } |