1/*
2 * Copyright (C) 2008 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 android.util.Log;
18import android.os.Debug;
19
20/**
21 * Loads a class, runs the garbage collector, and prints showmap output.
22 *
23 * <p>Usage: dalvikvm LoadClass [class name]
24 */
25class LoadClass {
26
27    public static void main(String[] args) {
28        System.loadLibrary("android_runtime");
29
30        if (registerNatives() < 0) {
31            throw new RuntimeException("Error registering natives.");
32        }
33
34        Debug.startAllocCounting();
35
36        if (args.length > 0) {
37            try {
38                long start = System.currentTimeMillis();
39                Class.forName(args[0]);
40                long elapsed = System.currentTimeMillis() - start;
41                Log.i("LoadClass", "Loaded " + args[0] + " in " + elapsed
42                        + "ms.");
43            } catch (ClassNotFoundException e) {
44                Log.w("LoadClass", e);
45                return;
46            }
47        }
48
49        System.gc();
50
51        int allocCount = Debug.getGlobalAllocCount();
52        int allocSize = Debug.getGlobalAllocSize();
53        int freedCount = Debug.getGlobalFreedCount();
54        int freedSize = Debug.getGlobalFreedSize();
55        long nativeHeapSize = Debug.getNativeHeapSize();
56
57        Debug.stopAllocCounting();
58
59        StringBuilder response = new StringBuilder("DECAFBAD");
60
61        int[] pages = new int[6];
62        Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo();
63        Debug.getMemoryInfo(memoryInfo);
64        response.append(',').append(memoryInfo.nativeSharedDirty);
65        response.append(',').append(memoryInfo.dalvikSharedDirty);
66        response.append(',').append(memoryInfo.otherSharedDirty);
67        response.append(',').append(memoryInfo.nativePrivateDirty);
68        response.append(',').append(memoryInfo.dalvikPrivateDirty);
69        response.append(',').append(memoryInfo.otherPrivateDirty);
70
71        response.append(',').append(allocCount);
72        response.append(',').append(allocSize);
73        response.append(',').append(freedCount);
74        response.append(',').append(freedSize);
75        response.append(',').append(nativeHeapSize);
76
77        System.out.println(response.toString());
78    }
79
80    /**
81     * Registers native functions. See AndroidRuntime.cpp.
82     */
83    static native int registerNatives();
84}
85