打印Android程序所占内存

Android项目中内存溢出,故关注程序内存使用情况。

写了个打印内存使用情况的脚本:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class CdMemoryReader {
	BufferedWriter writer;

	/**
	 * 
	 * @Description: TODO
	 * @param @param args args[0] : process name args[1]: execute time (int)
	 *        args[2]: sleep time (long)
	 * @return void
	 * @throws @param args
	 * @date:2010/06/19 author:
	 */
	public static void main(String args[]) {
		final String brower = "brower";
		final String mms = "mms";

		long sleeptime = 2000;
		int times = 1000;
		String procName = heco;

		// CdMemoryReader cdMemoryReader = new CdMemoryReader() ;
		System.out.println();
		switch (args.length) {
		default:
			System.out.println("the args more than 3 is useless !");
			System.out.println();
		case 3:
			try {
				long sleepT = Long.parseLong(args[2]);
				sleeptime = sleepT < 1000="" 1000="" :="" sleept;="" }="" catch="" (numberformatexception="" e)="" {="" system.out="" .println("the="" args:="" "="" +="" args[2]="" +="" "is="" not="" a="" number="" .");="" system.exit(0);="" }="" case="" 2:="" try="" {="" int="" exectime="Integer.parseInt(args[1]);" times="exectime"><= 0="" 1="" :="" exectime;="" }="" catch="" (numberformatexception="" e)="" {="" system.out="" .println("the="" args:="" "="" +="" args[1]="" +="" "is="" not="" a="" number="" .");="" system.exit(0);="" }="" case="" 1:="" string="" name="args[0];" 这里可以不做判断="" 直接使用传入的参数="" 我们是特定打印="" if="" (name.contains(brower))="" {="" procname="brower;" }="" else="" if="" (name.contains(mms))="" {="" procname="mms;" }="" else="" {="" system.out.println("the="" process="" name="" is="" wrong="" !!");="" system.exit(0);="" }="" case="" 0:="" system.out.println("proce="" name:="" "="" +="" procname);="" system.out.println();="" system.out.println("execute="" times:="" "="" +="" times);="" system.out.println();="" system.out.println("sleep="" time:="" "="" +="" sleeptime);="" system.out.println();="" new="" cdmemoryreader().cdaccess(sleeptime,="" times,="" 20,="" procname);="" break;="" }="" }="" private="" void="" cdaccess(long="" time,="" int="" times,="" int="" lines,="" string="" procname)="" {="" runtime="" rt="Runtime.getRuntime();" stringbuffer="" buffer="new" stringbuffer();="" string="" title=" PID      Vss      Rss      Pss      Uss  cmdline" ;="" system.out.println(title);="" try="" {="" writer="new" bufferedwriter(new="" filewriter(new="" file("d:/log.txt")));="" writer.write(title="" +="" "/n");="" }="" catch="" (ioexception="" e)="" {="" todo="" auto-generated="" catch="" block="" e.printstacktrace();="" }="" int="" printtime="1;" for="" (int="" i="0;" i="">< times;="" i++)="" {="" try="" {="" string="" cmd="adb shell procrank" ;="" buffer.delete(0,="" buffer.length());="" process="" proc="rt.exec(cmd);" inputstream="" stderr="proc.getInputStream();" inputstreamreader="" isr="new" inputstreamreader(stderr);="" bufferedreader="" br="new" bufferedreader(isr);="" string="" line="null;" int="" j="0;" while="" ((line="br.readLine())" !="null)" {="" if="" (j="">< lines)="" {="" if="" (line.contains(procname))="" {="" system.out.println(line="" +="" "="" printtime="" :="" "="" +="" printtime++="" +="" "="" execute="" time="" :="" "="" +="" (i="" +="" 1));="" buffer.append(line="" +="" "/n");="" break;="" }="" }="" j++;="" }="" buffer.append("/n");="" buffer.append("/n");="" writer.write(buffer.tostring());="" writer.flush();="" system.out.println("buffer.tostring()"="" +="" buffer.tostring());="" }="" catch="" (throwable="" t)="" {="" t.printstacktrace();="" }="" try="" {="" thread.sleep(time);="" }="" catch="" (interruptedexception="" ie)="" {="" ie.printstacktrace();="" }="" }="" try="" {="" writer.close();="" }="" catch="" (ioexception="" e)="" {="" todo="" auto-generated="" catch="" block="" e.printstacktrace();="" }="" }="" }="">

日志文件默认放在D盘。

将uss的数据获取放在excel中可以生成以下的图,更直观些。

打印所占内存

暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心