FaceBook推出的Android图片加载库-Fresco

FaceBook推出的Android图片加载库-Fresco

在Android设备上面,快速高效的显示图片是极为重要的。过去的几年里,我们在如何高效的存储图像这方面遇到了很多问题。图片太大,但是手机的内存却很小。每一个像素的R、G、B和alpha通道总共要占用4byte的空间。如果手机的屏幕是480*800,那么一张屏幕大小的图片就要占用1.5M的内存。手机的内存通常很小,特别是Android设备还要给各个应用分配内存。在某些设备上,分给Facebook App的内存仅仅有16MB。一张图片就要占据其内存的十分之一。

当你的App内存溢出会发生什么呢?它当然会崩溃!我们开发了一个库来解决这个问题,我们叫它Fresco。它可以管理使用到的图片和内存,从此App不再崩溃。

内存区

为了理解Facebook到底做了什么工作,在此之前我们需要了解在Android可以使用的堆内存之间的区别。Android中每个App的Java堆内存大小都是被严格的限制的。每个对象都是使用Java的new在堆内存实例化,这是内存中相对安全的一块区域。内存有垃圾回收机制,所以当App不在使用内存的时候,系统就会自动把这块内存回收。

不幸的是,内存进行垃圾回收的过程正是问题所在。当内存进行垃圾回收时,内存不仅仅进行了垃圾回收,还把 Android 应用完全终止了。这也是用户在使用 App 时最常见的卡顿或短暂假死的原因之一。这会让正在使用 App 的用户非常郁闷,然后他们可能会焦躁地滑动屏幕或者点击按钮,但 App 唯一的响应就是:在 App 恢复正常之前,请求用户耐心等待

相比之下,Native堆是由C++程序的new进行分配的。在Native堆里面有更多可用内存,App只被设备的物理可用内存限制,而且没有垃圾回收机制或其他东西拖后腿。但是c++程序员必须自己回收所分配的每一块内存,否则就会造成内存泄露,最终导致程序崩溃。

Android有另外一种内存区域,叫做Ashmem。它操作起来更像Native堆,但是也有额外的系统调用。Android 在操作 Ashmem 堆时,会把该堆中存有数据的内存区域从 Ashmem 堆中抽取出来,而不是把它释放掉,这是一种弱内存释放模式;被抽取出来的这部分内存只有当系统真正需要更多的内存时(系统内存不够用)才会被释放。当 Android 把被抽取出来的这部分内存放回 Ashmem 堆,只要被抽取的内存空间没有被释放,之前的数据就会恢复到相应的位置。

可消除的Bitmap

Ashmem不能被Java应用直接处理,但是也有一些例外,图片就是其中之一。当你创建一张没有经过压缩的Bitmap的时候,Android的API允许你指定是否是可清除的。

BitmapFactory.Options = new BitmapFactory.Options();
options.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length, options);
  • 1
  • 2
  • 3

经过上面的代码处理后,可清除的Bitmap会驻留在 Ashmem 堆中。不管发生什么,垃圾回收器都不会自动回收这些 Bitmap。当 Android 绘制系统在渲染这些图片,Android 的系统库就会把这些 Bitmap 从 Ashmem 堆中抽取出来,而当渲染结束后,这些 Bitmap 又会被放回到原来的位置。如果一个被抽取的图片需要再绘制一次,系统仅仅需要把它再解码一次,这个操作非常迅速。

这听起来像一个完美的解决方案,但是问题是Bitmap解码的操作是运行在UI线程的。Bitmap解码是非常消耗CPU资源的,当消耗过大时会引起UI阻塞。因为这个原因,所以Google不推荐使用这个特性。现在它们推荐使用另外一个特性——inBitmap。但是这个特性直到Android3.0之后才被支持。即使是这样,这个特性也不是非常有用,除非 App 里的所有图片大小都相同,这对Fackbook来说显然是不适用的。一直到4.4版本,这个限制才被移除了。但我们需要的是能够运行在 Android 2.3 - 最新版本中的通用解决方案。

http://www.meilijie.com/ask/view/377116/
http://www.meilijie.com/ask/view/377668/
http://www.meilijie.com/ask/view/377838/
http://www.meilijie.com/ask/view/378347/
http://www.meilijie.com/ask/view/378495/
http://www.meilijie.com/ask/view/379087/
http://www.meilijie.com/ask/view/379256/
http://www.meilijie.com/ask/view/379881/
http://www.meilijie.com/ask/view/380076/
http://www.meilijie.com/ask/view/380646/
http://www.meilijie.com/ask/view/380767/
http://www.meilijie.com/ask/view/381293/
http://www.meilijie.com/ask/view/381453/
http://www.meilijie.com/ask/view/382200/
http://www.meilijie.com/ask/view/382376/
http://www.meilijie.com/ask/view/382562/
http://www.meilijie.com/ask/view/382793/
http://www.meilijie.com/ask/view/382946/
http://www.meilijie.com/ask/view/383125/
http://www.meilijie.com/ask/view/383304/
http://www.meilijie.com/ask/view/383542/
http://www.meilijie.com/ask/view/383670/
http://www.meilijie.com/ask/view/383957/
http://www.meilijie.com/ask/view/384113/
http://www.meilijie.com/ask/view/384634/
http://www.meilijie.com/ask/view/384955/
http://www.meilijie.com/ask/view/385142/
http://www.meilijie.com/ask/view/385324/
http://www.meilijie.com/ask/view/385572/
http://www.meilijie.com/ask/view/385786/
http://www.meilijie.com/ask/view/386089/
http://www.meilijie.com/ask/view/386341/
http://www.meilijie.com/ask/view/386727/
http://www.meilijie.com/ask/view/386982/
http://www.meilijie.com/ask/view/387187/
http://www.meilijie.com/ask/view/387352/
http://www.meilijie.com/ask/view/387919/
http://www.meilijie.com/ask/view/388268/
http://www.meilijie.com/ask/view/388700/
http://www.meilijie.com/ask/view/389047/
http://www.meilijie.com/ask/view/389707/
http://www.meilijie.com/ask/view/390006/
http://www.meilijie.com/ask/view/390634/
http://www.meilijie.com/ask/view/390843/
http://www.meilijie.com/ask/view/391044/
http://www.meilijie.com/ask/view/391226/
http://www.meilijie.com/ask/view/391734/
http://www.meilijie.com/ask/view/391890/
http://www.meilijie.com/ask/view/392936/
http://www.meilijie.com/ask/view/393254/
http://www.meilijie.com/ask/view/393546/
http://www.meilijie.com/ask/view/393789/
http://www.meilijie.com/ask/view/394342/
http://www.meilijie.com/ask/view/394698/
http://www.meilijie.com/ask/view/395570/
http://www.meilijie.com/ask/view/395830/
http://www.meilijie.com/ask/view/395986/
http://www.meilijie.com/ask/view/397744/
http://www.meilijie.com/ask/view/398059/
http://www.meilijie.com/ask/view/398315/
http://www.meilijie.com/ask/view/398506/
http://www.meilijie.com/ask/view/399480/
http://www.meilijie.com/ask/view/400569/
http://www.meilijie.com/ask/view/401554/
http://www.meilijie.com/ask/view/401929/
http://www.meilijie.com/ask/view/402217/
http://www.meilijie.com/ask/view/402934/
http://www.meilijie.com/ask/view/403112/
http://my.tv.sohu.com/user/253531394
http://my.tv.sohu.com/user/253531395
http://my.tv.sohu.com/user/253531271
http://my.tv.sohu.com/user/253531802
http://my.tv.sohu.com/user/253531525
http://my.tv.sohu.com/user/253530999
http://my.tv.sohu.com/user/253531000
http://my.tv.sohu.com/user/253529600
http://my.tv.sohu.com/user/253531603
http://my.tv.sohu.com/user/253531399
http://my.tv.sohu.com/user/253531284
http://my.tv.sohu.com/user/253531529
http://my.tv.sohu.com/user/253531706
http://my.tv.sohu.com/user/253531289
http://my.tv.sohu.com/user/253531531
http://my.tv.sohu.com/user/253531708
http://my.tv.sohu.com/user/253531290
http://my.tv.sohu.com/user/253531812
http://my.tv.sohu.com/user/253531291
http://my.tv.sohu.com/user/253531711
http://my.tv.sohu.com/user/253531536
http://my.tv.sohu.com/user/253531537
http://my.tv.sohu.com/user/253531608
http://my.tv.sohu.com/user/253531539
http://my.tv.sohu.com/user/253531813
http://my.tv.sohu.com/user/253531540
http://my.tv.sohu.com/user/253531292
http://my.tv.sohu.com/user/253531293
http://my.tv.sohu.com/user/253531609
http://my.tv.sohu.com/user/253531611
http://my.tv.sohu.com/user/253531542
http://my.tv.sohu.com/user/253531716
http://my.tv.sohu.com/user/253531717
http://my.tv.sohu.com/user/253531296
http://my.tv.sohu.com/user/253531817
http://my.tv.sohu.com/user/253531618
http://my.tv.sohu.com/user/253531819
http://my.tv.sohu.com/user/253531299
http://my.tv.sohu.com/user/253531620
http://my.tv.sohu.com/user/253531902
http://my.tv.sohu.com/user/253530806
http://my.tv.sohu.com/user/253531822
http://my.tv.sohu.com/user/253531825
http://my.tv.sohu.com/user/253531826
http://my.tv.sohu.com/user/253531723
http://my.tv.sohu.com/user/253531547
http://my.tv.sohu.com/user/253531549
http://my.tv.sohu.com/user/253531550
http://my.tv.sohu.com/user/253531552
http://my.tv.sohu.com/user/253531909
http://my.tv.sohu.com/user/253529890
http://my.tv.sohu.com/user/253529892
http://my.tv.sohu.com/user/253531728
http://my.tv.sohu.com/user/253531626
http://my.tv.sohu.com/user/253531730
http://my.tv.sohu.com/user/253531731
http://my.tv.sohu.com/user/253531735
http://my.tv.sohu.com/user/253531736
http://my.tv.sohu.com/user/253531628
http://my.tv.sohu.com/user/253531737
http://my.tv.sohu.com/user/253531914
http://my.tv.sohu.com/user/253531915
http://my.tv.sohu.com/user/253529893
http://my.tv.sohu.com/user/253531631
http://my.tv.sohu.com/user/253531559
http://my.tv.sohu.com/user/253531740
http://my.tv.sohu.com/user/253531919
http://my.tv.sohu.com/user/253531920
http://my.tv.sohu.com/user/253531561
http://my.tv.sohu.com/user/253531838
http://my.tv.sohu.com/user/253531744
http://my.tv.sohu.com/user/253531635
http://my.tv.sohu.com/user/253531921
http://my.tv.sohu.com/user/253531840
http://my.tv.sohu.com/user/253531922
http://my.tv.sohu.com/user/253531746
http://my.tv.sohu.com/user/253531565
http://my.tv.sohu.com/user/253531925
http://my.tv.sohu.com/user/253531567
http://my.tv.sohu.com/user/253531642
http://my.tv.sohu.com/user/253531926
http://my.tv.sohu.com/user/253531568
http://my.tv.sohu.com/user/253531569
http://my.tv.sohu.com/user/253531928
http://my.tv.sohu.com/user/253531748
http://my.tv.sohu.com/user/253531847
http://my.tv.sohu.com/user/253531750
http://my.tv.sohu.com/user/253530817
http://my.tv.sohu.com/user/253531574
http://my.tv.sohu.com/user/253531576
http://my.tv.sohu.com/user/253531645
http://my.tv.sohu.com/user/253531577
http://my.tv.sohu.com/user/253531931
http://my.tv.sohu.com/user/253531578
http://my.tv.sohu.com/user/253531753
http://my.tv.sohu.com/user/253531580
http://my.tv.sohu.com/user/253531932
http://my.tv.sohu.com/user/253531650
http://my.tv.sohu.com/user/253531651
http://my.tv.sohu.com/user/253531854
http://my.tv.sohu.com/user/253531856
http://my.tv.sohu.com/user/253531937
http://my.tv.sohu.com/user/253531755
http://my.tv.sohu.com/user/253531757
http://my.tv.sohu.com/user/253531939
http://my.tv.sohu.com/user/253531758
http://my.tv.sohu.com/user/253531860
http://my.tv.sohu.com/user/253531861
http://my.tv.sohu.com/user/253531586
http://my.tv.sohu.com/user/253531107
http://my.tv.sohu.com/user/253531108
http://my.tv.sohu.com/user/253531658
http://my.tv.sohu.com/user/253531762
http://my.tv.sohu.com/user/253531587
http://my.tv.sohu.com/user/253531863
http://my.tv.sohu.com/user/253531590
http://my.tv.sohu.com/user/253531591
http://my.tv.sohu.com/user/253531593
http://my.tv.sohu.com/user/253531868
http://my.tv.sohu.com/user/253531110
http://my.tv.sohu.com/user/253531595
http://my.tv.sohu.com/user/253531773
http://my.tv.sohu.com/user/253531666
http://my.tv.sohu.com/user/253531874
http://my.tv.sohu.com/user/253531113
http://my.tv.sohu.com/user/253532005
http://my.tv.sohu.com/user/253532006
http://my.tv.sohu.com/user/253531777
http://my.tv.sohu.com/user/253531878
http://my.tv.sohu.com/user/253531778
http://my.tv.sohu.com/user/253531956
http://my.tv.sohu.com/user/253531116
http://my.tv.sohu.com/user/253532008
http://my.tv.sohu.com/user/253531117
http://my.tv.sohu.com/user/253532010
http://my.tv.sohu.com/user/253531957
http://my.tv.sohu.com/user/253531781
http://my.tv.sohu.com/user/253531782
http://my.tv.sohu.com/user/253531674
http://my.tv.sohu.com/user/253531783
http://my.tv.sohu.com/user/253530829
http://my.tv.sohu.com/user/253532013
http://my.tv.sohu.com/user/253531887
http://my.tv.sohu.com/user/253531970
http://my.tv.sohu.com/user/253531121
http://my.tv.sohu.com/user/253531122
http://my.tv.sohu.com/user/253532017
http://my.tv.sohu.com/user/253531123
http://my.tv.sohu.com/user/253531972
http://my.tv.sohu.com/user/253531892
http://my.tv.sohu.com/user/253531974
http://my.tv.sohu.com/user/253531977
http://my.tv.sohu.com/user/253531893
http://my.tv.sohu.com/user/253530835
http://my.tv.sohu.com/user/253531895
http://my.tv.sohu.com/user/253531978
http://my.tv.sohu.com/user/253531679
http://my.tv.sohu.com/user/253531680
http://my.tv.sohu.com/user/253531796
http://my.tv.sohu.com/user/253532021
http://my.tv.sohu.com/user/253532022
http://my.tv.sohu.com/user/253530837
http://my.tv.sohu.com/user/253532024
http://my.tv.sohu.com/user/253532025
http://my.tv.sohu.com/user/253531981
http://my.tv.sohu.com/user/253531898
http://my.tv.sohu.com/user/253531899
http://my.tv.sohu.com/user/253531798
http://my.tv.sohu.com/user/253531984
http://my.tv.sohu.com/user/253531985
http://my.tv.sohu.com/user/253532027
http://my.tv.sohu.com/user/253531900
http://my.tv.sohu.com/user/253531684
http://my.tv.sohu.com/user/253532406
http://my.tv.sohu.com/user/253531687
http://my.tv.sohu.com/user/253531688
http://my.tv.sohu.com/user/253531127
http://my.tv.sohu.com/user/253530839
http://my.tv.sohu.com/user/253532031
http://my.tv.sohu.com/user/253532408
http://my.tv.sohu.com/user/253531988
http://my.tv.sohu.com/user/253532411
http://my.tv.sohu.com/user/253532033
http://my.tv.sohu.com/user/253532412
http://my.tv.sohu.com/user/253531128
http://my.tv.sohu.com/user/253532105
http://my.tv.sohu.com/user/253532036
http://my.tv.sohu.com/user/253532417
http://my.tv.sohu.com/user/253531995
http://my.tv.sohu.com/user/253532111
http://my.tv.sohu.com/user/253532419
http://my.tv.sohu.com/user/253531694
http://my.tv.sohu.com/user/253530842
http://my.tv.sohu.com/user/253531998
http://my.tv.sohu.com/user/253531696
http://my.tv.sohu.com/user/253532000
http://my.tv.sohu.com/user/253532421
http://my.tv.sohu.com/user/253532423
http://my.tv.sohu.com/user/253532043
http://my.tv.sohu.com/user/253532425
http://my.tv.sohu.com/user/253531698
http://my.tv.sohu.com/user/253531699
http://my.tv.sohu.com/user/253532047
http://my.tv.sohu.com/user/253532508
http://my.tv.sohu.com/user/253532428
http://my.tv.sohu.com/user/253532048
http://my.tv.sohu.com/user/253532430
http://my.tv.sohu.com/user/253532431
http://my.tv.sohu.com/user/253532119
http://my.tv.sohu.com/user/253532049
http://my.tv.sohu.com/user/253532432
http://my.tv.sohu.com/user/253532433
http://my.tv.sohu.com/user/253532120
http://my.tv.sohu.com/user/253532436
http://my.tv.sohu.com/user/253532053
http://my.tv.sohu.com/user/253532516
http://my.tv.sohu.com/user/253532518
http://my.tv.sohu.com/user/253532441
http://my.tv.sohu.com/user/253530846
http://my.tv.sohu.com/user/253532442
http://my.tv.sohu.com/user/253532445
http://my.tv.sohu.com/user/253532521
http://my.tv.sohu.com/user/253532057
http://my.tv.sohu.com/user/253532058
http://my.tv.sohu.com/user/253532213
http://my.tv.sohu.com/user/253532214
http://my.tv.sohu.com/user/253532522
http://my.tv.sohu.com/user/253531134
http://my.tv.sohu.com/user/253532061
http://my.tv.sohu.com/user/253532448
http://my.tv.sohu.com/user/253532449
http://my.tv.sohu.com/user/253532450
http://my.tv.sohu.com/user/253532219
http://my.tv.sohu.com/user/253532221
http://my.tv.sohu.com/user/253532067
http://my.tv.sohu.com/user/253532524
http://my.tv.sohu.com/user/253532130
http://my.tv.sohu.com/user/253532222
http://my.tv.sohu.com/user/253531135
http://my.tv.sohu.com/user/253532069
http://my.tv.sohu.com/user/253532454
http://my.tv.sohu.com/user/253532225
http://my.tv.sohu.com/user/253532071
http://my.tv.sohu.com/user/253532228
http://my.tv.sohu.com/user/253532297
http://my.tv.sohu.com/user/253532743
http://my.tv.sohu.com/user/253532931
http://my.tv.sohu.com/user/253532744
http://my.tv.sohu.com/user/253532746
http://my.tv.sohu.com/user/253532933
http://my.tv.sohu.com/user/253532824
http://my.tv.sohu.com/user/253532599
http://my.tv.sohu.com/user/253532934
http://my.tv.sohu.com/user/253533105
http://my.tv.sohu.com/user/253533107
http://my.tv.sohu.com/user/253532826
http://my.tv.sohu.com/user/253533005
http://my.tv.sohu.com/user/253533006
http://my.tv.sohu.com/user/253532749
http://my.tv.sohu.com/user/253532936
http://my.tv.sohu.com/user/253531171
http://my.tv.sohu.com/user/253532938
http://my.tv.sohu.com/user/253532752
http://my.tv.sohu.com/user/253532940
http://my.tv.sohu.com/user/253533111
http://my.tv.sohu.com/user/253533112
http://my.tv.sohu.com/user/253533113
http://my.tv.sohu.com/user/253532831
http://my.tv.sohu.com/user/253533014
http://my.tv.sohu.com/user/253532832
http://my.tv.sohu.com/user/253532833
http://my.tv.sohu.com/user/253533015
http://my.tv.sohu.com/user/253532944
http://my.tv.sohu.com/user/253533017
http://my.tv.sohu.com/user/253533018
http://my.tv.sohu.com/user/253532755
http://my.tv.sohu.com/user/253532946
http://my.tv.sohu.com/user/253532836
http://my.tv.sohu.com/user/253533019
http://my.tv.sohu.com/user/253530886
http://my.tv.sohu.com/user/253532838
http://my.tv.sohu.com/user/253532950
http://my.tv.sohu.com/user/253530888
http://my.tv.sohu.com/user/253533024
http://my.tv.sohu.com/user/253532951
http://my.tv.sohu.com/user/253533122
http://my.tv.sohu.com/user/253533027
http://my.tv.sohu.com/user/253533029
http://my.tv.sohu.com/user/253531175
http://my.tv.sohu.com/user/253532758
http://my.tv.sohu.com/user/253532842
http://my.tv.sohu.com/user/253531176
http://my.tv.sohu.com/user/253532760
http://my.tv.sohu.com/user/253532957
http://my.tv.sohu.com/user/253532845
http://my.tv.sohu.com/user/253532762
http://my.tv.sohu.com/user/253532847
http://my.tv.sohu.com/user/253532763
http://my.tv.sohu.com/user/253530889
http://my.tv.sohu.com/user/253532849
http://my.tv.sohu.com/user/253533131
http://my.tv.sohu.com/user/253532851
http://my.tv.sohu.com/user/253533035
http://my.tv.sohu.com/user/253532770
http://my.tv.sohu.com/user/253533134
http://my.tv.sohu.com/user/253530891
http://my.tv.sohu.com/user/253533038
http://my.tv.sohu.com/user/253532966
http://my.tv.sohu.com/user/253532771
http://my.tv.sohu.com/user/253533040
http://my.tv.sohu.com/user/253533041
http://my.tv.sohu.com/user/253533139
http://my.tv.sohu.com/user/253532968
http://my.tv.sohu.com/user/253532857
http://my.tv.sohu.com/user/253533043
http://my.tv.sohu.com/user/253532774
http://my.tv.sohu.com/user/253533044
http://my.tv.sohu.com/user/253533045
http://my.tv.sohu.com/user/253532859
http://my.tv.sohu.com/user/253533143
http://my.tv.sohu.com/user/253533047
http://my.tv.sohu.com/user/253531181
http://my.tv.sohu.com/user/253533144
http://my.tv.sohu.com/user/253532780
http://my.tv.sohu.com/user/253532781
http://my.tv.sohu.com/user/253532865
http://my.tv.sohu.com/user/253532786
http://my.tv.sohu.com/user/253533054
http://my.tv.sohu.com/user/253531185
http://my.tv.sohu.com/user/253533150
http://my.tv.sohu.com/user/253532787
http://my.tv.sohu.com/user/253532788
http://my.tv.sohu.com/user/253533056
http://my.tv.sohu.com/user/253532789
http://my.tv.sohu.com/user/253532980
http://my.tv.sohu.com/user/253533058
http://my.tv.sohu.com/user/253532790
http://my.tv.sohu.com/user/253533152
http://my.tv.sohu.com/user/253533059
http://my.tv.sohu.com/user/253533153
http://my.tv.sohu.com/user/253531187
http://my.tv.sohu.com/user/253532981
http://my.tv.sohu.com/user/253533060
http://my.tv.sohu.com/user/253531189
http://my.tv.sohu.com/user/253533061

对于上面提到的“解码操作致使 UI 假死”的问题,我们找到了一种同时使 UI 显示和内存管理都表现良好的解决方法。如果我们在 UI 线程进行渲染之前把被抽取的内存区域放回到原来的位置,并确保它再也不会被抽取,那我们就可以把这些图片放在 Ashmem 里,同时不会出现 UI 假死的问题。幸运的是,Android 的 NDK 中有一个函数可以完美地实现这个需求,名字叫做 AndroidBitmap_lockPixels。这个函数最初的目的就是:在调用 unlockPixels 再次抽取内存区域后被执行。

当我们意识到我们没有必要这样做的时候,我们取得了突破。如果我们只调用lockPixels而不调用对应的unlockPixels,那么我们就可以在Java的堆内存里面创建一个内存安全的图像,并且不会导致UI线程加载缓慢。只需要几行c++代码,我们就完美的解决了这个问题。

就像《蜘蛛侠》里面说的:“能力越强,责任越大。”可清除的 Bitmap 既不会被垃圾回收器回收,也不会被 Ashmem 内置的清除机制处理,这使得使用它们可能会造成内存泄露。所以我们只能靠自己啦。

在c++中,通常的解决方案是建立智能指针类,实现引用计数。这些需要利用到c++的语言特性——拷贝构造函数、赋值操作符和确定的析构函数。这种语法在Java之中不存在,因为垃圾回收器能够处理这一切。所以我们必须以某种方式在Java中实现C++的这些保证机制。

我们创建了两个类去完成这件事。其中一个叫做“SharedReference”,它有addReference和deleteReference两个方法,调用者调用时必须采取基类对象或让它在范围之外。一旦引用计数器归零,资源处理(Bitmap.recycle)就会发生。

然而,很显然,让Java开发者去调用这些方法是很容易出错的。Java语言就是为了避免做这样的事情的!所以SharedReference之上,我们构建了CloseableReference类。它不仅实现了Java的Closeable接口,而且也实现了Cloneable接口。它的构造器和clone()方法会调用addReference(),而close()方法会调用deleteReference()。所以Java开发者需要遵守下面两条简单的的规则:

  1. 在分配CloseableReference新对象的时候,调用.clone()。
  2. 在超出作用域范围的时候,调用.close(),这通常是在finally代码块中。

这些规则可以有效地防止内存泄漏,并让我们在像Fackbook的Android客户端这种大型的Java程序中享受Native内存管理和通信。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。