说在前面的话(可忽略)
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();
}