Debugging example for Android Development

Source: Internet
Author: 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;

/**

* Set the debugging mode.

*

* @ Param bIsDebug

*/

Public static void setMode (boolean bIsDebug)

{

M_bIsDebug = bIsDebug;

}

/**

* Debug mode or not

* @ Return

*/

Public static boolean isDebug ()

{

Return m_bIsDebug;

}

/**

* Print information

*

* @ Param strTxt

*/

Public static void println (String strTxt)

{

If (m_bIsDebug)

{

System. out. println (strTxt );

}

}

/**

* Print information

*

* @ Param strTxt

*/

Public static void Log (String strTag, String strTxt)

{

If (m_bIsDebug)

{

Log. I (strTag, strTxt );

}

}

/**

* Forcibly recycle garbage, which can be used to detect destructor and detect whether unused objects have

*/

Public static void gc ()

{

If (m_bIsDebug)

{

System. gc ();

}

}

/**

* Print the total heap volume.

*/

Public static void printTotalMemory ()

{

Runtime r = Runtime. getRuntime ();

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

}

/**

* Print heap remaining amount

*/

Public static void printFreeMemory ()

{

Gc (); // execute force reclaim to obtain accurate residual amount

Runtime r = Runtime. getRuntime ();

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

}

/**

* Print heap changes

*/

Static long longPre = 0;

Public static void printVaryMemory ()

{

Gc (); // execute force reclaim to obtain accurate residual amount

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;

}

}

/**

* Monitoring memory

*

* @ 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;

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.