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;
}
}
}