老婆保佑,代码无BUG

前言

面试题:Java中的软引用,弱引用在Android 是哪个的使用


目录

  • 一:哪四种引用
  • 二:区别在哪
  • 三:在Android中的使用场景
      1. Handler 弱引用,防止内存泄漏
      1. Bitmap软引用防止OOM
  • 参考

一:哪四种引用

  • 强引用(Strong Reference)
  • 软引用(Soft Reference)
  • 弱引用(WeakReference)
  • 虚引用

二:区别在哪

1. 强引用(Strong Reference)

强引用是指在程序代码中普遍存在的,类似“Object obj=new Object()”这类的引用,只要强引用还存在,垃圾收集器永远不会回收掉被引用的对象。

只要某个对象有强引用与之关联,JVM必定不会回收这个对象,即使在内存不足的情况下,JVM宁愿抛出OutOfMemory错误也不会回收这种对象

2. 软引用(Soft Reference)

软引用是用来描述一些有用但并不是必需的对象。对于软引用关联着的对象,只有在内存不足的时候JVM才会回收该对象。因此,这一点可以很好地用来解决OOM的问题,并且这个特性很适合用来实现缓存:比如网页缓存、图片缓存等。JDK 1.2之后,提供了SoftReference类来实现软引用。

import java.lang.ref.SoftReference; public class Main {    public static void main(String[] args) {                 SoftReference sr = new SoftReference(new String("hello"));        System.out.println(sr.get());    }}

3.弱引用(WeakReference)

弱引用也是用来描述非必需对象的,当JVM进行垃圾回收时,无论内存是否充足,都会回收被弱引用关联的对象

import java.lang.ref.WeakReference; public class Main {    public static void main(String[] args) {             WeakReference sr = new WeakReference(new String("hello"));                 System.out.println(sr.get());        System.gc();                //通知JVM的gc进行垃圾回收        System.out.println(sr.get());    }}

输出结果

hellonull

4.虚引用(PhantomReference)

虚引用和前面的软引用、弱引用不同,它并不影响对象的生命周期。在java中用java.lang.ref.PhantomReference类表示。如果一个对象与虚引用关联,则跟没有引用与之关联一样,在任何时候都可能被垃圾回收器回收。

import java.lang.ref.PhantomReference;import java.lang.ref.ReferenceQueue;  public class Main {    public static void main(String[] args) {        ReferenceQueue queue = new ReferenceQueue();        PhantomReference pr = new PhantomReference(new String("hello"), queue);        System.out.println(pr.get());    }}

总结

对比 强引用 软引用 弱引用 虚引用
引用强度 最强 第二 第三 最弱
如何使用 new Object() SoftReference WeakReference PhantomReference

三:在Android中的使用场景

1. Handler 弱引用,防止内存泄漏

 import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import java.lang.ref.WeakReference; public class MainActivity extends AppCompatActivity {     private Handler handler  ;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         handler = new MyHandler( this ) ;         new Thread(new Runnable() {            @Override            public void run() {               handler.sendEmptyMessage( 0 ) ;            }        }).start() ;     }     private static class MyHandler extends Handler {        WeakReference weakReference ;         public MyHandler(MainActivity activity ){            weakReference  = new WeakReference( activity) ;        }         @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            if ( weakReference.get() != null ){                // update android ui            }        }    } }

2. Bitmap软引用防止OOM

下面这段代码是摘自博客:

http://blog.csdn.net/arui319/article/details/8489451

private Map> imageCache = new HashMap>();
....public void addBitmapToCache(String path) {// 强引用的Bitmap对象Bitmap bitmap = BitmapFactory.decodeFile(path);// 软引用的Bitmap对象SoftReference softBitmap = new SoftReference(bitmap);// 添加该对象到Map中使其缓存imageCache.put(path, softBitmap);}public Bitmap getBitmapByPath(String path) {// 从缓存中取软引用的Bitmap对象SoftReference softBitmap = imageCache.get(path);// 判断是否存在软引用if (softBitmap == null) {return null;}// 取出Bitmap对象,如果由于内存不足Bitmap被回收,将取得空Bitmap bitmap = softBitmap.get();return bitmap;}

参考

http://blog.csdn.net/sunxianghuang/article/details/52267282

https://www.cnblogs.com/dolphin0520/p/3784171.html

https://www.cnblogs.com/zhaoyanjun/p/5977190.html

更多相关文章

  1. android fw systemserver之MountService分析
  2. 跨进程调用Service(AIDL Service)
  3. Android(安卓)Handler 泄漏
  4. Android四大组件之ContentProvider(一)
  5. Android应该掌握的高级技巧
  6. android存储本地数据大全
  7. 第十三篇 Android(安卓)系统电话管理机制与架构二
  8. Android(安卓)Junit
  9. IPC 机制(上)

随机推荐

  1. android 自定义导航控件
  2. android获取经纬度和地方名称
  3. android 定位服务
  4. android绘图之Paint(1)
  5. Android开发05—Android常用高级控件(下)
  6. android中HttpURLConnection调用getRespo
  7. Windows环境下Android Studio系列4—界面
  8. android中MotionEvent.ACTION_CANCEL事件
  9. Android初级教程_在电脑上共享手机屏幕
  10. Android SDK自带教程之BluetoothChat