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 | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Program used to create a heap dump for test purposes. |
| 25 | */ |
| 26 | public class Main { |
| 27 | // Keep a reference to the DumpedStuff instance so that it is not garbage |
| 28 | // collected before we take the heap dump. |
| 29 | public static DumpedStuff stuff; |
| 30 | |
| 31 | // We will take a heap dump that includes a single instance of this |
| 32 | // DumpedStuff class. Objects stored as fields in this class can be easily |
| 33 | // found in the hprof dump by searching for the instance of the DumpedStuff |
| 34 | // class and reading the desired field. |
| 35 | public static class DumpedStuff { |
| 36 | public String basicString = "hello, world"; |
| 37 | public String nullString = null; |
| 38 | public Object anObject = new Object(); |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 39 | public ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>(); |
| 40 | public PhantomReference aPhantomReference = new PhantomReference(anObject, referenceQueue); |
| 41 | public WeakReference aWeakReference = new WeakReference(anObject, referenceQueue); |
Richard Uhler | 1af86f1 | 2015-10-29 14:55:00 -0700 | [diff] [blame] | 42 | public byte[] bigArray; |
| 43 | |
| 44 | DumpedStuff() { |
| 45 | int N = 1000000; |
| 46 | bigArray = new byte[N]; |
| 47 | for (int i = 0; i < N; i++) { |
| 48 | bigArray[i] = (byte)((i*i) & 0xFF); |
| 49 | } |
| 50 | } |
Richard Uhler | 3524472 | 2015-09-10 16:45:54 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public static void main(String[] args) throws IOException { |
| 54 | if (args.length < 1) { |
| 55 | System.err.println("no output file specified"); |
| 56 | return; |
| 57 | } |
| 58 | String file = args[0]; |
| 59 | |
| 60 | // Allocate the instance of DumpedStuff. |
| 61 | stuff = new DumpedStuff(); |
| 62 | |
| 63 | // Take a heap dump that will include that instance of DumpedStuff. |
| 64 | System.err.println("Dumping hprof data to " + file); |
| 65 | VMDebug.dumpHprofData(file); |
| 66 | } |
| 67 | } |