频道栏目
IT货架 > > 正文
android内存检测
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:内存   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();
    }

android内存检测

标签:内存   android应用   检验   

相关板块推荐 iPhone WindowsPhone

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1