安卓開發之調試執行個體

來源:互聯網
上載者:User

   package AndroidApi;

  import android.util.Log;

  class Monitoring implements Runnable

  {

  public void run()

  {

  while (!Thread.currentThread().isInterrupted())

  {

  try

  {

  Thread.sleep(100);

  } catch (InterruptedException s)

  {

  Thread.currentThread().interrupt();

  }

  AndroidDebug.printVaryMemory();

  }

  }

  }

  public class AndroidDebug

  {

  private static boolean m_bIsDebug = false;

  /**

  * 設定偵錯模式

  *

  * @param bIsDebug

  */

  public static void setMode(boolean bIsDebug)

  {

  m_bIsDebug = bIsDebug;

  }

  /**

  * 是否偵錯模式

  * @return

  */

  public static boolean isDebug()

  {

  return m_bIsDebug;

  }

  /**

  * 列印資訊

  *

  * @param strTxt

  */

  public static void println(String strTxt)

  {

  if (m_bIsDebug)

  {

  System.out.println(strTxt);

  }

  }

  /**

  * 列印資訊

  *

  * @param strTxt

  */

  public static void Log(String strTag, String strTxt)

  {

  if (m_bIsDebug)

  {

  Log.i(strTag,strTxt);

  }

  }

  /**

  * 強制回收垃圾,可用於檢測解構函式,檢測未使用對象是否有

  */

  public static void gc()

  {

  if (m_bIsDebug)

  {

  System.gc();

  }

  }

  /**

  * 列印堆總量

  */

  public static void printTotalMemory()

  {

  Runtime r = Runtime.getRuntime();

  AndroidDebug.println("Total memory is :" + r.totalMemory());

  }

  /**

  * 列印堆剩餘量

  */

  public static void printFreeMemory()

  {

  gc(); // 執行強制回收以獲得準確的剩餘量

  Runtime r = Runtime.getRuntime();

  AndroidDebug.println("Free memory is :" + r.freeMemory());

  }

  /**

  * 列印堆變化量

  */

  static long longPre = 0;

  public static void printVaryMemory()

  {

  gc(); // 執行強制回收以獲得準確的剩餘量

  Runtime r = Runtime.getRuntime();

  long longNow = r.freeMemory();

  if (longNow > longPre)

  {

  AndroidDebug.println("Free memory -> :" + (longNow - longPre));

  longPre = longNow;

  } else if (longNow < longPre)

  {

  AndroidDebug.println("Free memory <- :" + (longPre - longNow));

  longPre = longNow;

  }

  }

  /**

  * 監控記憶體

  *

  * @param bIsOpen

  */

  private static Thread m_pThread = null;

  public static void setMonitore(boolean bIsOpen)

  {

  if (bIsOpen)

  {

  if (null == m_pThread)

  m_pThread = new Thread(new Monitoring());

  m_pThread.setDaemon(true);

  m_pThread.start();

  }

  else

  {

  if (null != m_pThread)

  {

  m_pThread.interrupt();

  m_pThread = null;

  }

  }

  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.