内存泄露会导致很多问题,程序卡顿频繁出发GC,OOM(数组下标越界),内存占用过大,直接被系统回收(目前有些手机的系统版本对于高内存应用,会直接杀死)App在内存泄露的时候一般会直接崩溃。所以Android在内存的性能优化上是至关重要的一环。
内存优化方案 5R:(对目前的优化方案的总结)
1.Reckon(计算)
通过计算等方式,获取到自己程序中的内存占用情况,然后在自己的程序中合理分配。
2.Reduce(减少)
通过更加高效的设计模式或者数据结构来减少内存的使用。
1、使用更加轻量的数据结构。(使用ArrayMap/SparseArray而不是HashMap等传统数据结构)
2、避免在Android里面使用Enum。(由于枚举或占用比静态常量13倍的空间,一个字段的枚举约500字节的空间)
3、减少Bitmap等资源类对象的内存占用。(即对资源进行缩减,比如图像的缩放比例和解码格式的选择)
4、减少Service的使用。
5、程序中字符串的拼接,多使用StringBuilder(线程不安全但高效)代替“+”。(当然多线程调用的时候,可以使用StringBuffer)
3.Reuse(重用)
1、Bitmap等资源对象的复用。
2、线程池的使用。(使用线程池能够高效的管理Android程序中的线程)
3、避免在循环中或者调用率高的函数中创建对象,可能会引起内存抖动。(内存抖动指内存频繁的分配和回收,GC导致的内存崩溃)
4.Recycle(回收)
1、对于不使用的对象内存,主动调用GC。(但是具体会不会回收,无法确定)
2、对强、软、弱、虚引用的合理规划使用。(在处理内存占用大的对象时候,可以可考虑使用软引用或弱引用)
5.Review(检查)
程序中静态内部类、匿名内部类、Thread、Handler、Timer Tasks、数据库Cursor等若没有及时做好关闭都容易造成内存泄露。(建议:设计程序的时候,对一些类的设计应当都做好init()和dispose()等操作)
- 在Activity中可以复写onLowMemory()与onTrimMemory(int level)函数,并在这两个函数中执行对内存的检测。
关于onTrimMemory(int level)回调时候的level参数说明:
/**
* Level for {@link #onTrimMemory(int)}: the process is nearing the end
* of the background LRU list, and if more memory isn't found soon it will
* be killed.
*/
static final int TRIM_MEMORY_COMPLETE = 80;
/**
* Level for {@link #onTrimMemory(int)}: the process is around the middle
* of the background LRU list; freeing memory can help the system keep
* other processes running later in the list for better overall performance.
*/
static final int TRIM_MEMORY_MODERATE = 60;
/**
* Level for {@link #onTrimMemory(int)}: the process has gone on to the
* LRU list. This is a good opportunity to clean up resources that can
* efficiently and quickly be re-built if the user returns to the app.
*/
static final int TRIM_MEMORY_BACKGROUND = 40;
/**
* Level for {@link #onTrimMemory(int)}: the process had been showing
* a user interface, and is no longer doing so. Large allocations with
* the UI should be released at this point to allow memory to be better
* managed.
*/
static final int TRIM_MEMORY_UI_HIDDEN = 20;
/**
* Level for {@link #onTrimMemory(int)}: the process is not an expendable
* background process, but the device is running extremely low on memory
* and is about to not be able to keep any background processes running.
* Your running process should free up as many non-critical resources as it
* can to allow that memory to be used elsewhere. The next thing that
* will happen after this is {@link #onLowMemory()} called to report that
* nothing at all can be kept in the background, a situation that can start
* to notably impact the user.
*/
static final int TRIM_MEMORY_RUNNING_CRITICAL = 15;
/**
* Level for {@link #onTrimMemory(int)}: the process is not an expendable
* background process, but the device is running low on memory.
* Your running process should free up unneeded resources to allow that
* memory to be used elsewhere.
*/
static final int TRIM_MEMORY_RUNNING_LOW = 10;
/**
* Level for {@link #onTrimMemory(int)}: the process is not an expendable
* background process, but the device is running moderately low on memory.
* Your running process may want to release some unneeded resources for
* use elsewhere.
*/
static final int TRIM_MEMORY_RUNNING_MODERATE = 5;
1、当应用程序是缓存时的回调等级:
(1)TRIM_MEMORY_BACKGROUND 表示手机目前内存已经很低了,系统准备开始根据LRU缓存来清理进程。这个时候我们的程序在LRU缓存列表的最近位置,是不太可能被清理掉的,但这时去释放掉一些比较容易恢复的资源能够让手机的内存变得比较充足,从而让我们的程序更长时间地保留在缓存当中,这样当用户返回我们的程序时会感觉非常顺畅,而不是经历了一次重新启动的过程。
(2)TRIM_MEMORY_MODERATE 表示手机目前内存已经很低了,并且我们的程序处于LRU缓存列表的中间位置,如果手机内存还得不到进一步释放的话,那么我们的程序就有被系统杀掉的风险了。
(3)TRIM_MEMORY_COMPLETE 表示手机目前内存已经很低了,并且我们的程序处于LRU缓存列表的最边缘位置,系统会最优先考虑杀掉我们的应用程序,在这个时候应当尽可能地把一切可以释放的东西都进行释放。
2、当应用程序真正运行时的回调等级:
(1)TRIM_MEMORY_RUNNING_MODERATE 表示应用程序正常运行,并且不会被杀掉。但是目前手机的内存已经有点低了,系统可能会开始根据LRU缓存规则来去杀死进程了。
(2)TRIM_MEMORY_RUNNING_LOW 表示应用程序正常运行,并且不会被杀掉。但是目前手机的内存已经非常低了,我们应该去释放掉一些不必要的资源以提升系统的性能,同时这也会直接影响到我们应用程序的性能。
(3)TRIM_MEMORY_RUNNING_CRITICAL 表示应用程序仍然正常运行,但是系统已经根据LRU缓存规则杀掉了大部分缓存的进程了。这个时候我们应当尽可能地去释放任何不必要的资源,不然的话系统可能会继续杀掉所有缓存中的进程,并且开始杀掉一些本来应当保持运行的进程,比如说后台运行的服务。
3、当应用程序的所有UI界面被隐藏。
TRIM_MEMORY_UI_HIDDEN表示当用户点击了Home键或者Back键导致应用的UI界面不可见时,应该释放一些资源。
- 使用Square公司基于MAT开源的LeakCanary执行内存泄露的检测
具体使用方式可以查看这篇博文(写的不错)--https://www.jianshu.com/p/70b8c87ea877