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; 19 import org.apache.harmony.dalvik.ddmc.DdmVmInternal; 20 21 /** 22 * Program used to create a heap dump for test purposes. 23 */ 24 public class Main { 25 // Keep a reference to the DumpedStuff instance so that it is not garbage 26 // collected before we take the heap dump. 27 public static DumpedStuff stuff; 28 main(String[] args)29 public static void main(String[] args) throws IOException { 30 if (args.length < 1) { 31 System.err.println("no output file specified"); 32 return; 33 } 34 String file = args[0]; 35 36 // If a --base argument is provided, it means we should generate a 37 // baseline hprof file suitable for using in testing diff. 38 boolean baseline = args.length > 1 && args[1].equals("--base"); 39 40 // Enable allocation tracking so we get stack traces in the heap dump. 41 DdmVmInternal.enableRecentAllocations(true); 42 43 // Allocate the instance of DumpedStuff. 44 stuff = new DumpedStuff(baseline); 45 46 // Create a bunch of unreachable objects pointing to basicString for the 47 // reverseReferencesAreNotUnreachable test 48 for (int i = 0; i < 100; i++) { 49 stuff.basicStringRef = new Object[]{stuff.basicString}; 50 } 51 52 stuff.shouldNotGc(); 53 54 // Take a heap dump that will include that instance of DumpedStuff. 55 System.err.println("Dumping hprof data to " + file); 56 VMDebug.dumpHprofData(file); 57 } 58 } 59