android内存检测

 /**
     * 开启线程检测软件可用内存情况,软件退出后, 调用System.exit(0)该线程才会停止 *
     * 
     * @param sleep
     *            每隔一段时间检测内存
     * @param standard
     *            内存超过多少比例报警
     */
    public static void memoryWatch(final int sleep, final int standard)
    {
        if (sleep <= 0)
        {
            Log.e(TAG, "memoryWatch, invalid sleep = 0!");
            return;
        }

        if (standard <= 0 && standard > 100)
        {
            Log.e(TAG, "memoryWatch, invalid standard percent = " + standard);
            return;
        }

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                Log.d(TAG, "memoryWatch, start thread!");
                while (true)
                {
                    long divisor = 1024 * 1024;
                    long maxMem = Runtime.getRuntime().maxMemory();
                    long totalMem = Runtime.getRuntime().totalMemory();
                    long freeMem = Runtime.getRuntime().freeMemory();
                    int percent = (int) (totalMem * 100 / maxMem);
                    Log.d(TAG, "memoryWatch" + ", app maxMem = " + maxMem
                            / divisor + "M, totalMem = " + totalMem / divisor
                            + "M, freeMem = " + freeMem / divisor
                            + "M, percent = " + percent);
                    if (standard <= percent)
                    {
                        if (freeMem / divisor <= 2)
                        {
                            Log.e(TAG, "memoryWatch" + ", app maxMem = "
                                    + maxMem / divisor + "M, totalMem = "
                                    + totalMem / divisor + "M, freeMem = "
                                    + freeMem / divisor + "M, percent = "
                                    + percent);

                        }
                        else if (freeMem / divisor >= totalMem / (divisor * 2))
                        {
                            Log.e(TAG, "memoryWatch" + ", app maxMem = "
                                    + maxMem / divisor + "M, totalMem = "
                                    + totalMem / divisor + "M, freeMem = "
                                    + freeMem / divisor + "M, percent = "
                                    + percent + "%"
                                    + ", reset memory warn flag = true!");
                        }
                    }

                    try
                    {
                        Thread.sleep(sleep);
                    }
                    catch (InterruptedException e)
                    {
                        Log.e(TAG, "memoryWatch, thread sleep exception : " + e);
                    }
                }
            }
        }).start();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值