Java引用类型详解

来源:互联网 发布:淘宝网ipad保护套 编辑:程序博客网 时间:2024/05/22 08:16

强引用( Final Reference)

就是指在程序代码中普遍存在的,类似Object obj = new Object()这类的引用,只要强引用还存在,垃圾收集器永远不会回收掉被引用的对象。
强引用具备以下三个个特点:

<code>1. 强引用可以直接访问目标对象;2. 强引用锁指向的对象在任何时候都不会被系统回收。JVM宁愿抛出OOM异常也不回收强引用所指向的对象; 3. 强应用可能导致内存泄露; </code>

整个FinalReference类的定义如下(有些API中并没有加入FinalReference类的说明,只能看源码了):

1
2
3
4
5
6
7
packagejava.lang.ref;
/* Final references, used to implement finalization */
classFinalReference<T> extendsReference<T> {
    publicFinalReference(T referent, ReferenceQueue<? superT> q) {
        super(referent, q);
    }
}

从类定义中可以看出,只有一个构造函数,根据所给的对象的应用和应用队列构造一个强引用。

软引用(Soft Reference)

是用来描述一些还有用但并非必须的对象。对于软引用关联着的对象,在系统将要发生内存溢出异常之前,将会把这些对象列进回收范围之中进行第二次回收。如果这次回收还没有足够的内存,才会抛出内存溢出异常。
对于软引用关联着的对象,如果内存充足,则垃圾回收器不会回收该对象,如果内存不够了,就会回收这些对象的内存。在 JDK 1.2 之后,提供了 SoftReference 类来实现软引用。软引用可用来实现内存敏感的高速缓存。软引用可以和一个引用队列(ReferenceQueue)联合使用,如果软引用所引用的对象被垃圾回收器回收,Java虚拟机就会把这个软引用加入到与之关联的引用队列中。
案例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
packagecollections.ref;
 
importjava.lang.ref.Reference;
importjava.lang.ref.ReferenceQueue;
importjava.lang.ref.SoftReference;
 
publicclass SoftRefTest
{
    privatestatic ReferenceQueue<MyObject> softQueue = newReferenceQueue<>();
 
    publicstatic class MyObject{
 
        @Override
        protectedvoid finalize() throwsThrowable
        {
            super.finalize();
            System.out.println("MyObject's finalize called");
        }
 
        @Override
        publicString toString()
        {
            return"I am MyObject";
        }
    }
 
    publicstatic class CheckRefQueue implementsRunnable
    {
        Reference<MyObject> obj = null;
        @Override
        publicvoid run()
        {
            try
            {
                obj = (Reference<MyObject>)softQueue.remove();
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            if(obj != null)
            {
                System.out.println("Object for SoftReference is "+obj.get());
            }
        }
    }
 
    publicstatic void main(String[] args)
    {
        MyObject object = newMyObject();
        SoftReference<MyObject> softRef = newSoftReference<>(object,softQueue);
        newThread(newCheckRefQueue()).start();
 
        object = null;   //删除强引用
        System.gc();
        System.out.println("After GC: Soft Get= "+softRef.get());
        System.out.println("分配大块内存");
        byte[] b = newbyte[5*1024*928];
        System.out.println("After new byte[]:Soft Get= "+softRef.get());
        System.gc();
    }
}

运行参数1:

1
-Xmx5M

运行结果1:

1
2
3
4
5
After GC: Soft Get= I am MyObject
分配大块内存
MyObject's finalize called
ObjectforSoftReference is null
Afternewbyte[]:Soft Get= null

运行参数2:

1
-Xmx5M -XX:PrintGCDetails

运行结果2(关于GC日志可以查看《Java堆内存http://blog.csdn.net/u013256816/article/details/50764532》):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[GC [PSYoungGen: 680K->504K(2560K)] 680K->512K(6144K), 0.0040658secs] [Times: user=0.00sys=0.00, real=0.00secs]
[Full GC [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 8K->482K(3584K)] 512K->482K(6144K) [PSPermGen: 2491K->2490K(21504K)],0.0188479secs] [Times: user=0.01sys=0.00, real=0.02secs]
After GC: Soft Get= I am MyObject
分配大块内存
[GC [PSYoungGen: 123K->64K(2560K)] 605K->546K(7680K), 0.0004285secs] [Times: user=0.00sys=0.00, real=0.00secs]
[GC [PSYoungGen: 64K->64K(2560K)] 546K->546K(7680K), 0.0003019secs] [Times: user=0.00sys=0.00, real=0.00secs]
[Full GC [PSYoungGen: 64K->0K(2560K)] [ParOldGen: 482K->482K(4608K)] 546K->482K(7168K) [PSPermGen: 2493K->2493K(21504K)],0.0094748secs] [Times: user=0.02sys=0.00, real=0.01secs]
[GC [PSYoungGen: 0K->0K(2560K)] 482K->482K(7680K), 0.0003759secs] [Times: user=0.00sys=0.00, real=0.00secs]
[Full GC [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 482K->472K(5120K)] 482K->472K(7680K) [PSPermGen: 2493K->2493K(21504K)],0.0101017secs] [Times: user=0.06sys=0.00, real=0.01secs]
MyObject's finalize called
ObjectforSoftReference is null
Afternewbyte[]:Soft Get= null
[GC [PSYoungGen: 122K->32K(2560K)] 5235K->5144K(7680K), 0.0004806secs] [Times: user=0.00sys=0.00, real=0.00secs]
[Full GC [PSYoungGen: 32K->0K(2560K)] [ParOldGen: 5112K->5112K(5120K)] 5144K->5112K(7680K) [PSPermGen: 2493K->2493K(21504K)],0.0136270secs] [Times: user=0.06sys=0.00, real=0.01secs]
Heap
 PSYoungGen      total 2560K, used 20K [0x00000000ffd00000,0x0000000100000000,0x0000000100000000)
  eden space 2048K, 1% used [0x00000000ffd00000,0x00000000ffd05250,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 ParOldGen       total 5120K, used 5112K [0x00000000ff800000,0x00000000ffd00000,0x00000000ffd00000)
  object space 5120K, 99% used [0x00000000ff800000,0x00000000ffcfe188,0x00000000ffd00000)
 PSPermGen       total 21504K, used 2500K [0x00000000fa600000,0x00000000fbb00000,0x00000000ff800000)
  object space 21504K, 11% used [0x00000000fa600000,0x00000000fa871190,0x00000000fbb00000)

加入 -XX:PrintGCDetails参数运行可以更形象的看到GC回收的细节。
这个案例1中,首先构造MyObject对象,并将其赋值给object变量,构成强引用。然后使用SoftReference构造这个MyObject对象的软引用softRef,并注册到softQueue引用队列。当softRef被回收时,会被加入softQueue队列。设置obj=null,删除这个强引用,因此,系统内对MyObject对象的引用只剩下软引用。此时,显示调用GC,通过软引用的get()方法,取得MyObject对象的引用,发现对象并未被回收,这说明GC在内存充足的情况下,不会回收软引用对象。
接着,请求一块大的堆空间5*1024*928,这个操作会使系统堆内存使用紧张,从而产生新一轮的GC。在这次GC后,softRef.get()不再返回MyObject对象,而是返回null,说明在系统内存紧张的情况下,软引用被回收。软引用被回收时,会被加入注册的引用队列。
如果将上面案例中的数组再改大点,比如5*1024*1024,就会抛出OOM异常:

1
2
3
4
5
6
After GC: Soft Get= I am MyObject
分配大块内存
MyObject's finalize called
ObjectforSoftReference is null
Exception in thread "main"java.lang.OutOfMemoryError: Java heap space
    at collections.ref.SoftRefTest.main(SoftRefTest.java:58)

软引用主要应用于内存敏感的高速缓存,在android系统中经常使用到。一般情况下,Android应用会用到大量的默认图片,这些图片很多地方会用到。如果每次都去读取图片,由于读取文件需要硬件操作,速度较慢,会导致性能较低。所以我们考虑将图片缓存起来,需要的时候直接从内存中读取。但是,由于图片占用内存空间比较大,缓存很多图片需要很多的内存,就可能比较容易发生OutOfMemory异常。这时,我们可以考虑使用软引用技术来避免这个问题发生。SoftReference可以解决oom的问题,每一个对象通过软引用进行实例化,这个对象就以cache的形式保存起来,当再次调用这个对象时,那么直接通过软引用中的get()方法,就可以得到对象中中的资源数据,这样就没必要再次进行读取了,直接从cache中就可以读取得到,当内存将要发生OOM的时候,GC会迅速把所有的软引用清除,防止oom发生。
案例2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
publicclass BitMapManager {
    privateMap<String, SoftReference<Bitmap>> imageCache = newHashMap<String, SoftReference<Bitmap>>();
 
    //保存Bitmap的软引用到HashMap
    publicvoid saveBitmapToCache(String path) {
        // 强引用的Bitmap对象
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        // 软引用的Bitmap对象
        SoftReference<Bitmap> softBitmap = newSoftReference<Bitmap>(bitmap);
        // 添加该对象到Map中使其缓存
        imageCache.put(path, softBitmap);
        // 使用完后手动将位图对象置null
        bitmap = null;
    }
 
    publicBitmap getBitmapByPath(String path) {
 
        // 从缓存中取软引用的Bitmap对象
        SoftReference<Bitmap> softBitmap = imageCache.get(path);
        // 判断是否存在软引用
        if(softBitmap == null) {
            returnnull;
        }
        // 取出Bitmap对象,如果由于内存不足Bitmap被回收,将取得空
        Bitmap bitmap = softBitmap.get();
        returnbitmap;
    }
}

弱引用(Weak Reference)

用来描述非必须的对象,但是它的强度比软引用更弱一些,被弱引用关联的对象只能生存到下一次垃圾收集发送之前。当垃圾收集器工作时,无论当前内存是否足够,都会回收掉只被弱引用关联的对象。一旦一个弱引用对象被垃圾回收器回收,便会加入到一个注册引用队列中。
我们略微修改一下案例1的代码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
packagecollections.ref;
 
importjava.lang.ref.Reference;
importjava.lang.ref.ReferenceQueue;
importjava.lang.ref.WeakReference;
 
publicclass WeakRefTest
{
    privatestatic ReferenceQueue<MyObject> weakQueue = newReferenceQueue<>();
 
    publicstatic class MyObject{
 
        @Override
        protectedvoid finalize() throwsThrowable
        {
            super.finalize();
            System.out.println("MyObject's finalize called");
        }
 
        @Override
        publicString toString()
        {
            return"I am MyObject";
        }
    }
 
    publicstatic class CheckRefQueue implementsRunnable
    {
        Reference<MyObject> obj = null;
        @Override
        publicvoid run()
        {
            try
            {
                obj = (Reference<MyObject>)weakQueue.remove();
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            if(obj != null)
            {
                System.out.println("删除的弱引用为:"+obj+"  but获取弱引用的对象obj.get()="+obj.get());
            }
        }
    }
 
    publicstatic void main(String[] args)
    {
        MyObject object = newMyObject();
        Reference<MyObject> weakRef = newWeakReference<>(object,weakQueue);
        System.out.println("创建的弱引用为:"+weakRef);
        newThread(newCheckRefQueue()).start();
 
        object = null;
        System.out.println("Before GC: Weak Get= "+weakRef.get());
        System.gc();
        System.out.println("After GC: Weak Get= "+weakRef.get());
    }
}

不加参数运行结果:

1
2
3
4
5
创建的弱引用为:java.lang.ref.WeakReference@29e07d3e
Before GC: Weak Get= I am MyObject
After GC: Weak Get= null
MyObject's finalize called
删除的弱引用为:java.lang.ref.WeakReference@29e07d3e but获取弱引用的对象obj.get()=null

可以看到,在GC之前,弱引用对象并未被垃圾回收器发现,因此通过 weakRef.get()可以获取对应的对象引用。但是只要进行垃圾回收,弱引用一旦被发现,便会立即被回收,并加入注册引用队列中。此时再试图通过weakRef.get()获取对象的引用就会失败。
弱引用的相关实际案例可以参考WeakHashMap,博主会在近期整理出相关文档。等不及的小伙伴可以自行度娘之。

软引用、弱引用都非常适合来保存那些可有可无的缓存数据。如果这么做,当系统内存不足时,这些缓存数据会被回收,不会导致内存溢出。而当内存资源充足时,这些缓存数据又可以存在相当长的时间,从而起来加速系统的作用。


虚引用(Phantom Reference)

虚引用也称为幽灵引用或者幻影引用,它是最弱的一种引用关系。一个持有虚引用的对象,和没有引用几乎是一样的,随时都有可能被垃圾回收器回收。当试图通过虚引用的get()方法取得强引用时,总是会失败。并且,虚引用必须和引用队列一起使用,它的作用在于跟踪垃圾回收过程。
虚引用中get方法的实现如下:

1
2
3
publicT get() {
       returnnull;
   }

可以看到永远返回null.
我们再来修改一下案例1的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
packagecollections.ref;
 
importjava.lang.ref.PhantomReference;
importjava.lang.ref.Reference;
importjava.lang.ref.ReferenceQueue;
importjava.util.concurrent.TimeUnit;
 
publicclass PhantomRefTest
{
    privatestatic ReferenceQueue<MyObject> phanQueue = newReferenceQueue<>();
 
    publicstatic class MyObject{
 
        @Override
        protectedvoid finalize() throwsThrowable
        {
            super.finalize();
            System.out.println("MyObject's finalize called");
        }
 
        @Override
        publicString toString()
        {
            return"I am MyObject";
        }
    }
 
    publicstatic class CheckRefQueue implementsRunnable
    {
        Reference<MyObject> obj = null;
        @Override
        publicvoid run()
        {
            try
            {
                obj = (Reference<MyObject>)phanQueue.remove();
                System.out.println("删除的虚引用为:"+obj+"  but获取虚引用的对象obj.get()="+obj.get());
                System.exit(0);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
 
    publicstatic void main(String[] args) throwsInterruptedException
    {
        MyObject object = newMyObject();
        Reference<MyObject> phanRef = newPhantomReference<>(object,phanQueue);
        System.out.println("创建的虚引用为:"+phanRef);
        newThread(newCheckRefQueue()).start();
 
        object = null;
        TimeUnit.SECONDS.sleep(1);
        inti =1;
        while(true)
        {
            System.out.println("第"+i+++"次gc");
            System.gc();
            TimeUnit.SECONDS.sleep(1);
        }
    }
}

运行结果:

1
2
3
4
5
创建的虚引用为:java.lang.ref.PhantomReference@3a6646fc
1次gc
MyObject's finalize called
2次gc
删除的虚引用为:java.lang.ref.PhantomReference@3a6646fc but获取虚引用的对象obj.get()=null

可以看到,再经过一次GC之后,系统找到了垃圾对象,并调用finalize()方法回收内存,但没有立即加入回收队列。第二次GC时,该对象真正被GC清楚,此时,加入虚引用队列。
虚引用的最大作用在于跟踪对象回收,清理被销毁对象的相关资源。
通常当对象不被使用时,重载该对象的类的finalize方法可以回收对象的资源。但是如果使用不慎,会使得对象复活,譬如这么编写finalize方法:

1
2
3
4
5
6
7
8
publicclass Test{
    publicstatic Test obj;
 
    @Overrideprotected void finalize() throwsThrowable{
        super.finalize();
        obj = this;
    }
}

对上面这个类Test中obj = new Test();然后obj=null;之后调用System.gc()企图销毁对象,但是很抱歉,不管你调用多少次System.gc()都没有什么用,除非你在下面的代码中再就obj=null;这样才能回收对象,这是因为JVM对某一个对象至多只执行一次被重写的finalize方法。
上面的小片段说明重写finalize的方法并不是很靠谱,可以使用虚引用来清理对象所占用的资源。
如下代码所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
   publicclass PhantomRefTest2
{
    privatestatic ReferenceQueue<MyObject> phanQueue = newReferenceQueue<>();
    privatestatic Map<Reference<MyObject>,String> map = newHashMap<>();
 
    publicstatic class MyObject{
 
        @Override
        protectedvoid finalize() throwsThrowable
        {
            super.finalize();
            System.out.println("MyObject's finalize called");
        }
 
        @Override
        publicString toString()
        {
            return"I am MyObject";
        }
    }
 
    publicstatic class CheckRefQueue implementsRunnable
    {
        Reference<MyObject> obj = null;
        @Override
        publicvoid run()
        {
            try
            {
                obj = (Reference<MyObject>)phanQueue.remove();
                Object value = map.get(obj);
                System.out.println("clean resource:"+value);
                map.remove(obj);
 
                System.out.println("删除的虚引用为:"+obj+"  but获取虚引用的对象obj.get()="+obj.get());
                System.exit(0);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
 
    publicstatic void main(String[] args) throwsInterruptedException
    {
        MyObject object = newMyObject();
        Reference<MyObject> phanRef = newPhantomReference<>(object,phanQueue);
        System.out.println("创建的虚引用为:"+phanRef);
        newThread(newCheckRefQueue()).start();
        map.put(phanRef,"Some Resources");
 
        object = null;
        TimeUnit.SECONDS.sleep(1);
        inti =1;
        while(true)
        {
            System.out.println("第"+i+++"次gc");
            System.gc();
            TimeUnit.SECONDS.sleep(1);
        }
    }
}

运行结果:

1
2
3
4
5
6
创建的虚引用为:java.lang.ref.PhantomReference@6a07348e
1次gc
MyObject's finalize called
2次gc
clean resource:Some Resources
删除的虚引用为:java.lang.ref.PhantomReference@6a07348e but获取虚引用的对象obj.get()=null

参考资料:
1. 《Java程序性能优化——让你的Java程序更快、更稳定》葛一鸣 等编著。
2. 《Java堆内存http://blog.csdn.net/u013256816/article/details/50764532》


原创粉丝点击