====== Android内存泄露 ======
虽说Android程序是用JAVA写的,并且在Dalvik VM中实现了JAVA的内存自动回收机制,但还是不可避免的会发生内存泄露,而且JAVA的内存泄露看起来更是诡异,如果是C写的发生内存泄露后,只要去仔细地去找分配内存的地方,再找到释放的地方是否一一对应,而且也有很多工具可以帮助你找到内存泄露.但一旦用到JAVA,就会自然觉得内存回收是虚拟机的事情,与我何干?也许正是由于JAVA的内存回收机制在我们的心目中过于强大,所以往往也就不会太在意内存的使用和回收了,写Android应用程序亦如此,至少我到现在都没怎么注意内存的回收问题.
当然一般情况下不会出现很严重的内存泄露,Android允许每个进程分配最多40M的内存,不太会因为内存使用过多而FC,但当一个进程中需要存在多个全屏的Widget,而且每个Widget都是ListView/GridView组成,并且要显示很多图片,这40M内存就显得有点太少了,如果再发生一些内存泄露,那会随便切下屏幕就有可能产生Out of memory的Exception,然后也就是经常的FC了,所以在这样的情况下管理好内存是一件很重要的事情.
====== 转载 ======
转载一些文章到我的wiki.
- [[android:mem-leak:Android-Mem-Leak-Test|Android内存泄露测试]]
- [[android:mem-leak:Android-Mem-Manager|Android内存管理]]
- [[android:mem-leak:Android-Traceview-Avoid-Mem-Leak|避免内存泄露, 有效利用内存]]
====== 参考链接 ======
- [[http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html|Avoiding memory leaks]]
- [[http://android-developers.blogspot.com/2009/02/track-memory-allocations.html|Track memory allocations]]
- [[http://www.eclipse.org/mat/|Memory Analyzer (MAT) For Eclipse]]
- [[http://stackoverflow.com/questions/1147172/what-android-tools-and-methods-work-best-to-find-memory-resource-leaks|What Android tools and methods work best to find memory/resource leaks?]]
======代码 ======
===== 打印内存代码 =====
private void printMemInfo(String TAG) {
TAG += ":meminfo";
logI(TAG, "printMemInfo:");
logI(TAG, "Max heap size = " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M");
logI(TAG, "Allocate heap size = " + android.os.Debug.getNativeHeapAllocatedSize() / 1024 + "K");
Method _readProclines = null;
try {
Class procClass;
procClass = Class.forName("android.os.Process");
Class parameterTypes[] = new Class[]{String.class, String[].class, long[].class};
_readProclines = procClass.getMethod("readProcLines", parameterTypes);
Object arglist[] = new Object[3];
final String[] mMemInfoFields = new String[]{"MemTotal:", "MemFree:", "Buffers:", "Cached:"};
long[] mMemInfoSizes = new long[mMemInfoFields.length];
mMemInfoSizes[0] = 30;
mMemInfoSizes[1] = -30;
arglist[0] = new String("/proc/meminfo");
arglist[1] = mMemInfoFields;
arglist[2] = mMemInfoSizes;
if (_readProclines != null) {
_readProclines.invoke(null, arglist);
for (int i = 0; i < mMemInfoSizes.length; i++) {
logI(TAG, mMemInfoFields[i] + " = " + mMemInfoSizes[i] / 1024 + "M");
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
===== unbindDrawables=====
unbindDrawables(activity.findViewById(R.id.root_view));
System.gc();
/**
* Unbind all the drawables.
* @param view
*/
private void unbindDrawables(View view) {
if (view != null) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
}