package Android.Test;
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
{
@SuppressWarnings("unused")
private static boolean m_bIsDebug = false;
/**
* 设置调试模式
*
* @param bIsDebug
*/
@SuppressWarnings("unused")
public static void setMode(boolean bIsDebug)
{
m_bIsDebug = bIsDebug;
}
/**
* 打印信息
*
* @param strTxt
*/
public static void println(String strTxt)
{
if (m_bIsDebug)
{
System.out.println(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;
}
}
}
}
//----------------------------- 测试释放问题 ---------------------
package Android.Test;
public class TestObject
{
String m_szTxt = "";
public TestObject(String szTxt)
{
// TODO Auto-generated constructor stub
m_szTxt = szTxt;
}
@Override
protected void finalize() throws Throwable
{
// TODO Auto-generated method stub
super.finalize();
System.out.println(m_szTxt);
}
}
//--------------------------------------------------
package Android.Test;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity
{
private TestObject m_pTestObject = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
androidDebug.setMode(true);
androidDebug.setMonitore(true);
androidDebug.println("Test music onCreate");
m_pTestObject = new TestObject("M object");
TestObject pTestObject = new TestObject("- object");
}
@Override
protected void finalize() throws Throwable
{
androidDebug.println("Main finalize");
// TODO Auto-generated method stub
super.finalize();
m_pTestObject = null;
}
@Override
protected void onDestroy()
{
androidDebug.println("Test music onDestroy");
// TODO Auto-generated method stub
super.onDestroy();
m_pTestObject = null;
//System.exit(0);
}
}
/*
* 测试结果:
* 1, 类变量如果未置空,不能释放。
* 2, 函数变量如果未置空, 可以被释放。
* 3, 监控内存时,如果不强制垃圾回收,会导致监控数据不准确。
*/