实例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产中图表,比便更直观的进行分析。