Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android內存檢測

android內存檢測

編輯:關於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();
    }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved