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

我们知道从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源码分析-软引用的黑科技)