Android中getprop查看内存、setprop修改内存

在android系统中,有一些初始化的配置文件,例如:

/init.rc

/default.prop

/system/build.prop

文件里面里面配置了开机设置的系统属性值,

这些属性值,可以通过getprop获取,setprop设置

 一、adb命令查询当前堆内存信息: 


1、查询所有配置:

adb shell getprop

2、如果你对某个属性名称不是那么确定的话就用下面的命令来过滤:

adb shell getprop | grep dalvik

  • dalvik.vm.heapgrowthlimit:表示进程默认虚拟机最大堆内存(单个应用可用最大内存,APP运行超出此限制就会OOM,但是仅仅针对dalvik堆,不包括native堆);
  • dalvik.vm.heapstartsize:它表示堆分配的初始大小,它会影响到整个系统对RAM的使用程度,和第一次使用应用时的流畅程度。它值越小,系统ram消耗越慢,但一些较大应用一开始不够用,需要调用gc和堆调整策略,导致应用反应较慢。它值越大,这个值越大系统ram消耗越快,但是应用更流畅(Android设备出厂以后,java虚拟机对单个应用的最大内存分配就确定下来了,超出这个值就会OOM。这个属性值是定义在/system/build.prop文件中的 dalvik.vm.heapstartsize=8m);
  • dalvik.vm.heapsize:表示单个进程可用的最大内存,但如果存在heapgrowthlimit参数,则以heapgrowthlimit为准;

3、通过代码查看每个进程可用的最大内存,即heapgrowthlimit值:

ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memClass = activityManager.getMemoryClass();//64,以m为单位

4、手动修改配置:

setprop可以对手机一些配置进行设置,当然这些配置必须是可写的。
命令格式:setprop [key] [value]
如果你想修改进程默认分配的可使用堆内存大小(大小不能超过dalvik.vm.heapsize的值):

adb shell setprop dalvik.vm.heapgrowthlimit 512m

5、watchprops

监听系统属性的变化,如果期间系统的属性发生变化则把变化的值显示出来

​​​注意:在设置了heapgrowthlimit的情况下,单个进程可用最大内存为heapgrowthlimit值。在android开发中,如果要使用大堆,需要在manifest中指定android:largeHeap为true,这样dvm heap最大可达heapsize。

 二、检查你应该使用多少的内存: 


正如前面提到的,每一个Android设备都会有不同的RAM总大小与可用空间,因此不同设备为app提供了不同大小的heap限制。你可以通过调用getMemoryClass())来获取你的app的可用heap大小。如果你的app尝试申请更多的内存,会出现OutOfMemory的错误。

在一些特殊的情景下,你可以通过在manifest的application标签下添加largeHeap=true的属性来声明一个更大的heap空间。如果你这样做,你可以通过getLargeMemoryClass())来获取到一个更大的heap size。

然而,能够获取更大heap的设计本意是为了一小部分会消耗大量RAM的应用(例如一个大图片的编辑应用)。不要轻易的因为你需要使用大量的内存而去请求一个大的heap size。只有当你清楚的知道哪里会使用大量的内存并且为什么这些内存必须被保留时才去使用large heap. 因此请尽量少使用large heap。使用额外的内存会影响系统整体的用户体验,并且会使得GC的每次运行时间更长。在任务切换时,系统的性能会变得大打折扣。

另外, large heap并不一定能够获取到更大的heap。在某些有严格限制的机器上,large heap的大小和通常的heap size是一样的。因此即使你申请了large heap,你还是应该通过执行getMemoryClass()来检查实际获取到的heap大小。

ActivityManager.java:   

 /**
     * Return the approximate per-application memory class of the current
     * device.  This gives you an idea of how hard a memory limit you should
     * impose on your application to let the overall system work best.  The
     * returned value is in megabytes; the baseline Android memory class is
     * 16 (which happens to be the Java heap limit of those devices); some
     * devices with more memory may return 24 or even higher numbers.
     */
    public int getMemoryClass() {
        return staticGetMemoryClass();
    }

    /** @hide */
    static public int staticGetMemoryClass() {
        // Really brain dead right now -- just take this from the configured
        // vm heap size, and assume it is in megabytes and thus ends with "m".
        String vmHeapSize = SystemProperties.get("dalvik.vm.heapgrowthlimit", "");
        if (vmHeapSize != null && !"".equals(vmHeapSize)) {
            return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length()-1));
        }
        return staticGetLargeMemoryClass();
    }

    /**
     * Return the approximate per-application memory class of the current
     * device when an application is running with a large heap.  This is the
     * space available for memory-intensive applications; most applications
     * should not need this amount of memory, and should instead stay with the
     * {@link #getMemoryClass()} limit.  The returned value is in megabytes.
     * This may be the same size as {@link #getMemoryClass()} on memory
     * constrained devices, or it may be significantly larger on devices with
     * a large amount of available RAM.
     *
     * <p>This is the size of the application's Dalvik heap if it has
     * specified <code>android:largeHeap="true"</code> in its manifest.
     */
    public int getLargeMemoryClass() {
        return staticGetLargeMemoryClass();
    }

    /** @hide */
    static public int staticGetLargeMemoryClass() {
        // Really brain dead right now -- just take this from the configured
        // vm heap size, and assume it is in megabytes and thus ends with "m".
        String vmHeapSize = SystemProperties.get("dalvik.vm.heapsize", "16m");
        return Integer.parseInt(vmHeapSize.substring(0, vmHeapSize.length() - 1));
    }

三、一些参数说明: 

 

  • dalvik.vm.heapgrowthlimit:默认给进程分配的可使用堆内存
  • dalvik.vm.heapsize:设置了android:largeHeap以后可使用的内存大小
  • ro.product.brand:手机品牌
  • ro.product.device:设备名称
  • ro.product.model:设备内部代号
  • ro.product.name:设备名称
  • ro.product.manufacturer:设备制造商
  • ro.serialno:设备序列号
  • ro.sf.lcd_density:设备屏幕密度
  • ro.config.ringtone:默认来电铃声
  • ro.config.notification_sound:默认通知铃声
  • ro.config.alarm_alert:默认闹钟铃声
  • dalvik.vm.stack-trace-file:trace文件放置目录


附:CmdRunUtil,在代码中执行cmd命令:
    

public class CmdRunUtil {
    private static final String TAG = "CmdRunUtil";

    public static void execCommand(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command);
        try {
            if (proc.waitFor() != 0) {
                System.err.println("exit value = " + proc.exitValue());
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    proc.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            String line = null;
            while ((line = in.readLine()) != null) {
                stringBuffer.append(line + " ");
            }

        } catch (InterruptedException e) {
            CatchUtil.handle(e);
            System.err.println(e);
        } finally {
            try {
                proc.destroy();
            } catch (Exception e2) {
            }
        }
    }


    /**
     * sync 指令
     * <p>
     * 将 缓冲区 数据 刷出
     */
    public static void sync() {
        Process localProcess = null;
        try {
            localProcess = Runtime.getRuntime().exec("sh");
            OutputStream localOutputStream = localProcess.getOutputStream();
            DataOutputStream local = new DataOutputStream(localOutputStream);
            local.writeBytes("sync\n");//sync, add by bill.
            local.flush();
            local.writeBytes("exit\n");
            local.flush();
            localProcess.waitFor();
            local.close();
        } catch (Exception e) {
            e.printStackTrace();
            CatchUtil.handle(e);
        } finally {
            if (localProcess != null) {
                localProcess.destroy();
            }
        }
    }


    /**
     * ping  baidu
     *
     * @return
     */
    public static boolean isPingInternet() {
        final String urlAddress = "www.baidu.com";
        String result = null;
        try {
            Process p = Runtime.getRuntime().exec("ping -w 3 " + urlAddress);
            // 读取ping的内容,可不加。
            InputStream input = p.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            StringBuffer stringBuffer = new StringBuffer();
            String content = "";
            while ((content = in.readLine()) != null) {
                stringBuffer.append(content);
            }
            // PING的状态
            int status = p.waitFor();
            if (status == 0) {
                LogM.log(LogType.Http, String.format("ping [%s]successful", urlAddress));
                return true;
            } else {
                LogM.log(LogType.Http, String.format("failed~ cannot reach the %s address", urlAddress));
                return false;
            }
        } catch (Exception e) {
            result = e.getLocalizedMessage();
            LogM.log(LogType.Http, "result = " + result);
        }
        return false;
    }
    }

调用示例:

 //修改app进程可以使用的堆内存大小
 CmdRunUtil.execCommand("setprop dalvik.vm.heapgrowthlimit 360m");

Thanks:

  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值