Android中如何查看内存(下)

实例1

int cnt=0;

final static int kBufferMinSize=1000;

final static int kBufferMaxSize=2000;

StringBuffer strBuffer=new StringBuffer(kBufferMinSize);

StringBuffer strBuffer2=new StringBuffer(kBufferMinSize);

StringBuffer strBuffer3=new StringBuffer(kBufferMinSize);

StringBuffer strBufferNativePss=new StringBuffer(kBufferMinSize);

StringBuffer strBufferDalvikPss=new StringBuffer(kBufferMinSize);

StringBuffer strBufferOtherPss=new StringBuffer(kBufferMinSize);

Debug.MemoryInfo memoryInfo=new Debug.MemoryInfo();

final static String tag="robin";

void printMemory()

{

long totalMemory=Runtime.getRuntime().totalMemory();

long freeMemory=Runtime.getRuntime().freeMemory();

long usedMemory=(totalMemory-freeMemory)>>10;

totalMemory=totalMemory>>10;

freeMemory=freeMemory>>10;

if(strBuffer.length()>kBufferMaxSize)

{

strBuffer.delete(0,strBuffer.length());

strBuffer2.delete(0,strBuffer2.length());

strBuffer3.delete(0,strBuffer3.length());

strBufferNativePss.delete(0,strBufferNativePss.length());

strBufferDalvikPss.delete(0,strBufferDalvikPss.length());

}

strBuffer.append(usedMemory+",");

strBuffer2.append(totalMemory+",");

strBuffer3.append((Debug.getNativeHeapSize()>>10)+",");

Debug.getMemoryInfo(memoryInfo);

strBufferNativePss.append((memoryInfo.nativePss)+",");

strBufferDalvikPss.append((memoryInfo.dalvikPss)+",");

if(cnt++%10==0)

{

Log.i(tag,"usedMemory:"+strBuffer.toString());

Log.i(tag,"totalMemory:"+strBuffer2.toString());

Log.i(tag,"NativeHeapSize:"+strBuffer3.toString());

Log.i(tag,"Native PSS:"+strBufferNativePss.toString());

Log.i(tag,"Dalvik PSS:"+strBufferDalvikPss.toString());

}

}

注意,对于输出的内存信息日志,我们稍作编辑就可以用于在excel产中图表,比便更直观的进行分析。

上一篇:python打开文件,python写入文件,python关闭文件,python读文件,python清空文件内容


下一篇:Hashset,Iterator