blob: 3936f296d3b96e0163be8abbd1f690b8c7412174 [file] [log] [blame]
Richard Uhler35244722015-09-10 16:45:54 -07001/*
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
17import dalvik.system.VMDebug;
18import java.io.IOException;
Richard Uhlerb3577302015-10-29 13:02:42 -070019import java.lang.ref.PhantomReference;
20import java.lang.ref.ReferenceQueue;
21import java.lang.ref.WeakReference;
Richard Uhler1a5baaa2015-12-21 12:47:26 -080022import libcore.util.NativeAllocationRegistry;
Richard Uhler35244722015-09-10 16:45:54 -070023
24/**
25 * Program used to create a heap dump for test purposes.
26 */
27public 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
32 // We will take a heap dump that includes a single instance of this
33 // DumpedStuff class. Objects stored as fields in this class can be easily
34 // found in the hprof dump by searching for the instance of the DumpedStuff
35 // class and reading the desired field.
36 public static class DumpedStuff {
37 public String basicString = "hello, world";
Richard Uhlerc7f77122016-02-11 08:48:24 -080038 public char[] charArray = "char thing".toCharArray();
Richard Uhler35244722015-09-10 16:45:54 -070039 public String nullString = null;
40 public Object anObject = new Object();
Richard Uhlerb3577302015-10-29 13:02:42 -070041 public ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>();
42 public PhantomReference aPhantomReference = new PhantomReference(anObject, referenceQueue);
43 public WeakReference aWeakReference = new WeakReference(anObject, referenceQueue);
Richard Uhler1af86f12015-10-29 14:55:00 -070044 public byte[] bigArray;
45
46 DumpedStuff() {
47 int N = 1000000;
48 bigArray = new byte[N];
49 for (int i = 0; i < N; i++) {
50 bigArray[i] = (byte)((i*i) & 0xFF);
51 }
Richard Uhler1a5baaa2015-12-21 12:47:26 -080052
Richard Uhler477e29a2016-05-03 13:05:50 -070053 NativeAllocationRegistry registry = new NativeAllocationRegistry(
54 Main.class.getClassLoader(), 0x12345, 42);
Richard Uhler1a5baaa2015-12-21 12:47:26 -080055 registry.registerNativeAllocation(anObject, 0xABCDABCD);
Richard Uhler1af86f12015-10-29 14:55:00 -070056 }
Richard Uhler35244722015-09-10 16:45:54 -070057 }
58
59 public static void main(String[] args) throws IOException {
60 if (args.length < 1) {
61 System.err.println("no output file specified");
62 return;
63 }
64 String file = args[0];
65
66 // Allocate the instance of DumpedStuff.
67 stuff = new DumpedStuff();
68
69 // Take a heap dump that will include that instance of DumpedStuff.
70 System.err.println("Dumping hprof data to " + file);
71 VMDebug.dumpHprofData(file);
72 }
73}