這可以看到目前可用的記憶體
ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
actvityManager.getMemoryInfo( mInfo );
Log.i( DT, " minfo.availMem " + mInfo.availMem );
Log.i( DT, " minfo.lowMemory " + mInfo.lowMemory );
Log.i( DT, " minfo.threshold " + mInfo.threshold );
要為程式做 Memory Leak 追蹤 依照下面找找看
1.Trace Memory Allocation
http://developer.android.com/resources/articles/track-mem.html
2.Trace Memory Leak in Native Code
http://groups.google.com/group/android-porting/browse_thread/thread/9c84baa10cebbb68?pli=1
http://discuz-android.blogspot.com/
Key words:
Automatic Memory Leak Tracking on Android
android memory usage with hprof
3.Avoid Memory Leak
http://developer.android.com/resources/articles/avoiding-memory-leaks.html
4.Common Memory Leak
Don't release field of Activity
When activity is closed with calling finish(), the finalize() method may not be called, the instance may stay in memory for re-use in future, but if memory is low, the activity will be released. So don't excpet GC to release field of Activity subclass, you must release them explicitly in onDestroy method. ie.
public void onDestroy()
{
super.onDestroy();
m_textField = null;
m_hashTable.clare();
m_hashTable = null;
}
Send messages in a wrong way
Correct:
Message message = handler.obtainMessage(); //handler is an instance of type Handler
//Set fields of message
message.id = xxx
message.obj = xxx
...
message.sendToTarget();
Wrong:
Message message = new Message();
message.id = xxx
message.obj = xxx
...
handler.sendMessage(message);
That forget to call close() method may cause memory leak
Cursor
IO Stream, such as InputStream/FileInputStream, OutputStream/FileOutputStream
Socket
Database
That forget to call clear() may cause memory leak
HashTable
Map and its subclass,
Collection and its subclass, such as List, ArrayList, LinkedList, Vector, Set, Stack, Queue,
原文
沒有留言:
張貼留言