UniversalImageLoader源码解读07-内存泄漏和bug

来源:互联网 发布:淘宝上开网店要钱吗 编辑:程序博客网 时间:2024/05/19 01:31

UIL使用单例模式

private volatile static ImageLoader instance;/** Returns singleton class instance */public static ImageLoader getInstance() {if (instance == null) {synchronized (ImageLoader.class) {if (instance == null) {instance = new ImageLoader();}}}return instance;}

如果单例模式没有与Activity关联,那么不至于泄漏Activity,不幸的是,UIL类内部持有了一个ImageLoaderConfiguration 引用,而我们在初始化的时候,传入了一个Context,

所以,如果在程序完全推出前,你不调用UIL的destroy方法,那么必然会导致Activity内存泄漏

public void destroy() {if (configuration != null) L.d(LOG_DESTROY);stop();configuration.diskCache.close();engine = null;configuration = null;//将Context与ImageLoader解除关联}

但是呢,destroy并没有把instance置空,美中不足,最好置为空。

destroy方法不是线程安全的,这也就意味着,在多线程环境下将会导致一些异常,这个异常你也许并不会陌生

private static final String ERROR_NOT_INIT = "ImageLoader must be init with configuration before using";


好, 到此为止,UniversalImageLoader的源码解读就算完成了,还有一些没有涉及到的工具类,我想不需要我做过多解释,太详细反倒使人感觉乏味,直击主题要害才是关键,谢谢大家

0 0
原创粉丝点击