An example of the commissioning of an Android development

Source: Internet
Author: User
Tags garbage collection thread

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 Debug mode

*

* @param bisdebug

*/

public static void SetMode (Boolean bisdebug)

{

M_bisdebug = Bisdebug;

}

/**

* Whether to debug mode

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

}

}

/**

* Forced garbage collection, which can be used to detect destructors, detect whether unused objects have

*/

public static void GC ()

{

if (M_bisdebug)

{

System.GC ();

}

}

/**

* Print Heap Total

*/

public static void Printtotalmemory ()

{

Runtime r = Runtime.getruntime ();

ANDROIDDEBUG.PRINTLN ("Total memory are:" + r.totalmemory ());

}

/**

* Print Heap Remaining Amount

*/

public static void Printfreememory ()

{

GC (); Perform a forced recycle to get the exact remaining amount

Runtime r = Runtime.getruntime ();

Androiddebug.println ("Free memory are:" + r.freememory ());

}

/**

* Print Heap Change volume

*/

static long Longpre = 0;

public static void Printvarymemory ()

{

GC (); Perform a forced recycle to get the exact remaining 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;

}

}

/**

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

}

}

}

Related Article

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.