android.os.Debug统计 GC 次数

本文介绍Android早期版本中VMDebug类提供的内存分配统计功能,包括开启和关闭统计的方法、获取GC次数等。这些特性有助于开发者理解应用程序的内存使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说在前面的话(可忽略)

API 8 中有 VMDebug 类
这里写图片描述

API19中已经去掉了 VMDebug 类(具体从哪个版本去掉的,没再找,如果你知道,希望留言告诉我)
这里写图片描述

这里写图片描述
早期版本中(如api 8) VMDebug 类提供了一些“access to some VM-specific debug”特性,但是文档中也明确说了,如果想统计这些信息最好通过 Debug 类。


正文

Start counting the number and aggregate size of memory allocations

1、开启统计相关信息

startAllocCounting
此方法先重置各种信息的数量,然后 开启统计

/**
     * Start counting the number and aggregate size of memory allocations.
     *
     * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting.
     * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis
     * code doesn't cause additional allocations.  The various <code>get</code> methods return
     * the specified value. And the various <code>reset</code> methods reset the specified
     * count.</p>
     *
     * <p>Counts are kept for the system as a whole (global) and for each thread.
     * The per-thread counts for threads other than the current thread
     * are not cleared by the "reset" or "start" calls.</p>
     *
     * @deprecated Accurate counting is a burden on the runtime and may be removed.
     */
    @Deprecated
    public static void startAllocCounting() {
        VMDebug.startAllocCounting();
    }
2、获取 GC 次数
/**
     * Returns the number of thread-local non-concurrent GC invocations between a
     * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
     */
    public static int getThreadGcInvocationCount() {
        return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
    }
/**
     * Returns the number of non-concurrent GC invocations between a
     * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
     */
    public static int getGlobalGcInvocationCount() {
        return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
    }
4、关闭统计
/**
     * Stop counting the number and aggregate size of memory allocations.
     *
     * @see #startAllocCounting()
     */
    @Deprecated
    public static void stopAllocCounting() {
        VMDebug.stopAllocCounting();
    }
----- pid 4846 at 2025-06-10 18:17:40.258314399+0800 ----- Cmd line: com.apportable.SosaVisitsAgbalumoFarm Build fingerprint: 'KORIDY/KYD-G2/KYD-G2:14/UP1A.231005.007/23217:userdebug/test-keys' ABI: 'arm' Build type: optimized suspend all histogram: Sum: 4.369ms 99% C.I. 4us-4183.040us Avg: 546.125us Max: 4321us DALVIK THREADS (23): "main" prio=5 tid=1 Waiting | group="main" sCount=1 ucsCount=0 flags=1 obj=0x7213b670 self=0xe63c4610 | sysTid=4846 nice=-20 cgrp=top-app sched=0/0 handle=0xf782346c | state=S schedstat=( 562428500 47866265 790 ) utm=41 stm=14 core=2 HZ=100 | stack=0xff733000-0xff735000 stackSize=8188KB | held mutexes= at java.lang.Object.wait(Native method) - waiting on <0x07abe4b0> (a android.os.Handler$BlockingRunnable) at java.lang.Object.wait(Object.java:386) at java.lang.Object.wait(Object.java:524) at android.os.Handler$BlockingRunnable.postAndWait(Handler.java:1030) - locked <0x07abe4b0> (a android.os.Handler$BlockingRunnable) at android.os.Handler.runWithScissors(Handler.java:630) at android.view.WindowManagerGlobal.setStoppedState(WindowManagerGlobal.java:705) at android.app.Activity.performStop(Activity.java:8882) at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:5480) at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:5460) at android.app.ActivityThread.handleStopActivity(ActivityThread.java:5525) at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:43) at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:180) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:98) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2535) at android.os.Handler.dispatchMessage(Handler.java:109) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8376) at java.lang.reflect.Method.invoke(Native method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:640) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)
06-11
08-05 15:41:03.347 1192 1192 D BluetoothHidHost: Proxy object disconnected 08-05 15:40:56.240 1232 1232 I chatty : uid=1000(system) ctc.android.smart.terminal.settings identical 2 lines 08-05 15:40:56.242 1232 1232 W ViewRootImpl[MoreActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=490.63416, y[0]=411.8772, toolType[0]=TOOL_TYPE_MOUSE, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=1100162, downTime=1098858, deviceId=5, source=0x2002 } 08-05 15:41:03.347 353 1309 I ActivityManager: Process com.android.bluetooth (pid 597) has died: psvc PER 08-05 15:41:03.347 353 353 D BluetoothManagerService: BluetoothServiceConnection, disconnected: com.android.bluetooth.btservice.AdapterService 08-05 15:41:03.348 353 405 W libprocessgroup: kill(-597, 9) failed: No such process 08-05 15:41:03.348 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.a2dp.A2dpService in 1000ms 08-05 15:41:03.348 353 415 E BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED(1) 08-05 15:41:03.348 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.btservice.AdapterService in 1000ms 08-05 15:41:03.348 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.avrcp.AvrcpTargetService in 11000ms 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.opp.BluetoothOppService in 11000ms 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.gatt.GattService in 11000ms 08-05 15:41:03.349 353 415 D BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED hasDevice:true mEnable:true 08-05 15:41:03.349 353 415 D BluetoothManagerService: Broadcasting onBluetoothServiceDown() to 5 receivers. 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.hid.HidDeviceService in 21000ms 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.hid.HidHostService in 20999ms 08-05 15:41:03.350 353 415 E BluetoothManagerService: Unable to call onBluetoothServiceDown() on callback #0 08-05 15:41:03.350 353 415 E BluetoothManagerService: android.os.DeadObjectException 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.BinderProxy.transactNative(Native Method) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.BinderProxy.transact(Binder.java:1129) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.bluetooth.IBluetoothManagerCallback$Stub$Proxy.onBluetoothServiceDown(IBluetoothManagerCallback.java:109) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.BluetoothManagerService.sendBluetoothServiceDownCallback(BluetoothManagerService.java:1356) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.BluetoothManagerService.access$4700(BluetoothManagerService.java:83) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.BluetoothManagerService$BluetoothHandler.handleMessage(BluetoothManagerService.java:1799) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.Handler.dispatchMessage(Handler.java:106) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.Looper.loop(Looper.java:193) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.HandlerThread.run(HandlerThread.java:65) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.ServiceThread.run(ServiceThread.java:44) 08-05 15:41:03.350 353 415 D BluetoothAdapter: onBluetoothServiceDown: android.bluetooth.IBluetooth$Stub$Proxy@d412eff 08-05 15:41:03.350 1232 1232 D BluetoothHidHost: Proxy object disconnected 08-05 15:41:03.350 3205 3239 D BluetoothAdapter: onBluetoothServiceDown: android.bluetooth.IBluetooth$Stub$Proxy@f8f674f 08-05 15:41:03.351 1232 1232 D BluetoothHidHost: Unbinding service... 08-05 15:41:03.351 353 415 D BluetoothManagerService: Sending BLE State Change: ON > TURNING_OFF
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐怡旸--指针诗笺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值