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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值