fresco源码分析-软引用的黑科技

作者: brycegao | 来源:发表于2016-12-24 10:57 被阅读0次

我们知道从Android2.*后google不推荐使用软引用了, 因为google优化了gc回收机制, 每次gc时不管内存是否充足都会释放软引用。 google推荐使用LruCache.Java替代软引用, 而LruCache内部维护个LinkedList, 实际上就是当内存不足时删掉最远使用的对象。

在分析fresco内存相关的源码时, 可以到有个类叫OOMSoftReferecne, 使用3个SoftReference指向同一个对象。 看最后一句翻译过来就是“只有在OOM时才会释放这个引用”TLDR: It's a reference that's cleared if and only if we otherwise would have encountered an OOM.翻译过来就是“只有在OOM时才会释放这个引用”, 多个软引用放到一起相当于强引用了。

/**

* To eliminate the possibility of some of our objects causing an OutOfMemoryError when they are

* not used, we reference them via SoftReferences.

* What is a SoftReference?

** A Soft Reference is a reference that is cleared when its referent is not strongly reachable and* there is memory pressure. SoftReferences as implemented by Dalvik blindly treat every second* SoftReference as a WeakReference every time a garbage collection happens, - i.e. clear it unless* there is something else referring to it:*dalvik*art* It will however clear every SoftReference if we don't have enough memory to satisfy an* allocation after a garbage collection.** This means that as long as one of the soft references stays alive, they all stay alive. If we* have two SoftReferences next to each other on the heap, both pointing to the same object, then* we are guaranteed that neither will be cleared until we otherwise would have thrown an* OutOfMemoryError.Since we can't strictly guarantee the location of objects on the heap, we use

* 3 just to be on the safe side.* TLDR: It's a reference that's cleared if and only if we otherwise would have encountered an OOM.*/public classOOMSoftReference {  SoftReferencesoftRef1;  SoftReferencesoftRef2;  SoftReferencesoftRef3;publicOOMSoftReference() {softRef1=null;softRef2=null;softRef3=null;  }public voidset(@NonnullThardReference) {softRef1=newSoftReference(hardReference);softRef2=newSoftReference(hardReference);softRef3=newSoftReference(hardReference);  }@NullablepublicTget() {return(softRef1==null?null:softRef1.get());  }public voidclear() {if(softRef1!=null) {softRef1.clear();softRef1=null;    }if(softRef2!=null) {softRef2.clear();softRef2=null;    }if(softRef3!=null) {softRef3.clear();softRef3=null;    }  }}

为了验证上述说法, 我写个简单的demo。

public classMainActivityextendsAppCompatActivity {    SoftReference softReference1;   

 SoftReference softReference2

 SoftReference softReference3;  

  TextView tvTest;

@Override

protected voidonCreate(Bundle savedInstanceState)    {

        super.onCreate(savedInstanceState);                     setContentView(R.layout.activity_main);      

  Bitmap bmp = Bitmap.createBitmap(2048,2048, Bitmap.Config.ARGB_8888);        Log.d("brycegao","bitmap size:"+         bmp.getByteCount());

softReference1=newSoftReference(bmp);/

/softReference2 = new SoftReference(bmp);

//softReference3 = new SoftReference(bmp);

tvTest= (TextView) findViewById(R.id.tv_test);

tvTest.setOnClickListener(newView.OnClickListener() {

   @Override

   public void  onClick(View v) {

                System.gc();  //强制内存回收            

               Object obj =softReference1.get();                                Log.d("brycegao","softreference:"+ obj);            }    

    });    }}

09-03 09:21:15.371 6530-6530/com.example.brycegao.myapplication D/brycegao: bitmap size:16777216

09-03 09:21:15.502 6530-7062/com.example.brycegao.myapplication V/RenderScript: 0x9ffde000 Launching thread(s), CPUs 4

09-03 09:21:24.872 6530-6530/com.example.brycegao.myapplication D/brycegao: softreference:null

从日志可以看出只有一个软饮用指向bitmap时, gc时会回收这块内存的。

放开softReference2,softReference3的注释, 即使用3个软引用再次测试

09-03 09:37:19.621 23036-23036/com.example.brycegao.myapplication D/brycegao: bitmap size:16777216

09-03 09:37:30.462 23036-23036/com.example.brycegao.myapplication D/brycegao: softreference:Android.graphics.Bitmap@3067335

果然!!! 软饮用指向的大图片没被回收, 以后在项目管理内存时可以使用这种做法, 即多个软饮用指向同一个对象

http://blog.csdn.net/brycegao321/article/details/52421293

相关文章

  • fresco源码分析-软引用的黑科技

    我们知道从Android2.*后google不推荐使用软引用了, 因为google优化了gc回收机制, 每次gc时...

  • Fresco源码分析-Fresco初始化

    本文为分析Fresco源码第一篇,基于fresco 1.8.1。 Fresco官网对于Fresco设计的基本概述 ...

  • Fresco图片显示原理浅析

    (第一篇)Fresco架构设计赏析 (第二篇)Fresco缓存架构分析 本文是Fresco源码分析系列第三篇文章,...

  • Fresco架构设计赏析

    本文是Fresco源码分析系列的开篇,主要分析Fresco的整体架构、各个组成模块的功能以及图片加载流程,希望通过...

  • 2019Android面试Fresco架构详解

    本文是Fresco源码分析系列的开篇,主要分析Fresco的整体架构、各个组成模块的功能以及图片加载流程,希望通过...

  • Fresco源码分析之Controller

    如果你是第一次看我的Fresco的源码分析系列文章,这里强烈推荐你先阅读我的前面两篇文章Fresco源码分析之Dr...

  • Fresco缓存设计分析

    (第一篇)Fresco架构设计赏析 本文是Fresco源码分析系列第二篇文章,主要来看一下Fresco中有关图片缓...

  • 软引用/弱引用/虚引用 源码分析及示例

    本篇文章的目的是要总结在Java当中常见的引用与垃圾回收之间的关系,主要介绍的包括引用的概念,引用队列的概念,弱引...

  • Glide源码(一)

    Picasso,Glide,Fresco对比 上一篇我们分析了Picasso,这一篇我们来分析Glide源码的调用...

  • Fresco源码分析-BitmapMemoryCacheProd

    前面几节大致了解了Fresco中的Producer,并分析了NetworkFetchProducer。接下来就大致...

网友评论

    本文标题:fresco源码分析-软引用的黑科技

    本文链接:https://www.haomeiwen.com/subject/ieplvttx.html