fresco源碼分析-軟引用的黑科技

我們知道從Android2.*后google不推薦使用軟引用了, 因?yàn)間oogle優(yōu)化了gc回收機(jī)制, 每次gc時(shí)不管內(nèi)存是否充足都會釋放軟引用。 google推薦使用LruCache.Java替代軟引用, 而LruCache內(nèi)部維護(hù)個(gè)LinkedList, 實(shí)際上就是當(dāng)內(nèi)存不足時(shí)刪掉最遠(yuǎn)使用的對象。

在分析fresco內(nèi)存相關(guān)的源碼時(shí), 可以到有個(gè)類叫OOMSoftReferecne, 使用3個(gè)SoftReference指向同一個(gè)對象。 看最后一句翻譯過來就是“只有在OOM時(shí)才會釋放這個(gè)引用”TLDR: It's a reference that's cleared if and only if we otherwise would have encountered an OOM.翻譯過來就是“只有在OOM時(shí)才會釋放這個(gè)引用”, 多個(gè)軟引用放到一起相當(dāng)于強(qiáng)引用了。

/**

* 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;? ? }? }}

為了驗(yàn)證上述說法, 我寫個(gè)簡單的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();? //強(qiáng)制內(nèi)存回收 ? ? ? ? ? ?

? ? ? ? ? ? ? ?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

從日志可以看出只有一個(gè)軟飲用指向bitmap時(shí), gc時(shí)會回收這塊內(nèi)存的。

放開softReference2,softReference3的注釋, 即使用3個(gè)軟引用再次測試

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

果然?。?! 軟飲用指向的大圖片沒被回收, 以后在項(xiàng)目管理內(nèi)存時(shí)可以使用這種做法, 即多個(gè)軟飲用指向同一個(gè)對象

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容