JVM之强软弱虚引用

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。JVM之强软弱虚引用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

在Java虚拟机(JVM)中,有几种不同类型的引用,它们分别是:强引用(Strong Reference)、软引用(Soft Reference)、弱引用(Weak Reference)、虚引用(Phantom Reference)和引用队列(Reference Queue)。这些引用类型提供了对对象的不同级别的引用,用于垃圾回收和内存管理。

1. 强引用(Strong Reference)

强引用是最常见的引用类型,它在程序中通过普通的对象引用方式进行声明。当一个对象具有强引用时,垃圾回收器不会回收该对象,即使内存不足也不会被回收。

示例代码:

Object obj = new Object(); // 创建一个强引用对象

我们平常一般使用的引用就是强引用。

2. 软引用(Soft Reference)

软引用是一种比强引用弱一些的引用。当内存不足时,垃圾回收器可能会回收具有软引用的对象,以释放内存。这使得软引用非常适合用于实现内存敏感的高速缓存。

代码示例:

public class a {

    public static void main(String[] args) throws InterruptedException {
        List<SoftReference<byte[]>> list=new ArrayList<>();
        for(int i=0;i<5;i++){
            SoftReference<byte[]> softReference=new SoftReference<>(new byte[4*1024*1024]);
            list.add(softReference);
        }
        for (SoftReference<byte[]> softReference : list) {
            System.out.println(softReference.get());
        }
    }

}

然后在vm options设置堆内存最大内存和打印垃圾回收信息参数如下:

-Xmx20m -XX:+PrintGCDetails -verbose:gc

打印日志如下:

[GC (Allocation Failure) [PSYoungGen: 2287K->488K(6144K)] 14575K->13060K(19968K), 0.0132177 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) –[PSYoungGen: 4584K->4584K(6144K)] 17156K->17212K(19968K), 0.0010759 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 4584K->4512K(6144K)] [ParOldGen: 12628K->12559K(13824K)] 17212K->17071K(19968K), [Metaspace: 3053K->3053K(1056768K)], 0.0043431 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) –[PSYoungGen: 4512K->4512K(6144K)] 17071K->17145K(19968K), 0.0008326 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 4512K->0K(6144K)] [ParOldGen: 12633K->672K(8704K)] 17145K->672K(14848K), [Metaspace: 3053K->3053K(1056768K)], 0.0036381 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
null
null
null
null
[B@ed17bee
Heap
 PSYoungGen      total 6144K, used 4378K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
  eden space 5632K, 77% used [0x00000000ff980000,0x00000000ffdc6828,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 ParOldGen       total 8704K, used 672K [0x00000000fec00000, 0x00000000ff480000, 0x00000000ff980000)
  object space 8704K, 7% used [0x00000000fec00000,0x00000000feca8218,0x00000000ff480000)
 Metaspace       used 3062K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 324K, capacity 386K, committed 512K, reserved 1048576K
Disconnected from the target VM, address: ‘127.0.0.1:63645’, transport: ‘socket’

当堆内存不足时,进行垃圾回收的时候,软引用对象就可以被垃圾回收。

3. 引用队列(Reference Queue)

引用队列是一个专门用于管理被垃圾回收器回收的引用对象的队列。当一个对象被回收时,与之关联的引用会被放入引用队列中,以便程序可以检测到对象的回收并执行相应的操作。

public class a {

    public static void main(String[] args) throws InterruptedException {
        List<SoftReference<byte[]>> list=new ArrayList<>();
        ReferenceQueue<byte[]> referenceQueue=new ReferenceQueue<>();
        for(int i=0;i<5;i++){
            SoftReference<byte[]> softReference=new SoftReference<>(new byte[4*1024*1024],referenceQueue);
            list.add(softReference);
        }
        Reference<? extends byte[]> poll = referenceQueue.poll();
        while (poll!=null){
            list.remove(poll);
            poll = referenceQueue.poll();

        }
        for (SoftReference<byte[]> softReference : list) {
            System.out.println(softReference.get());
        }
    }

}

使用引用队列后,就可以删除list集合中的为null的引用。打印日志不再出现null。

4. 弱引用(Weak Reference)

弱引用比软引用更弱一些。弱引用的对象在下一次垃圾回收时几乎肯定会被回收,无论内存是否足够。弱引用通常用于实现对象的临时引用,例如对象的临时计算结果缓存。

代码如下:

public class a {

    public static void main(String[] args) throws InterruptedException {
        List<WeakReference<byte[]>> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            WeakReference<byte[]> weakReference = new WeakReference<>(new byte[4 * 1024 * 1024]);
            list.add(weakReference);
            for (WeakReference<byte[]> reference : list) {
                System.out.print(reference.get()+" ");
            }
            System.out.println();
        }
        list.forEach(i-> System.out.println(i.get()));
    }

}

 打印日志如下:

[B@5caf905d 
[B@5caf905d [B@27716f4 
[B@5caf905d [B@27716f4 [B@8efb846 
[GC (Allocation Failure) [PSYoungGen: 2477K->496K(6144K)] 14765K->13219K(19968K), 0.0008677 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@5caf905d [B@27716f4 [B@8efb846 [B@2a84aee7 
[GC (Allocation Failure) [PSYoungGen: 4704K->480K(6144K)] 17428K->13227K(19968K), 0.0006313 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[B@5caf905d [B@27716f4 [B@8efb846 null [B@a09ee92 
[B@5caf905d [B@27716f4 [B@8efb846 null [B@a09ee92 Heap
 PSYoungGen      total 6144K, used 4745K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
  eden space 5632K, 75% used [0x00000000ff980000,0x00000000ffdaa478,0x00000000fff00000)
  from space 512K, 93% used [0x00000000fff80000,0x00000000ffff8020,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 13824K, used 12747K [0x00000000fec00000, 0x00000000ff980000, 0x00000000ff980000)
  object space 13824K, 92% used [0x00000000fec00000,0x00000000ff872fa8,0x00000000ff980000)
 Metaspace       used 3352K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 366K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0
 

在运行过程中每次打印信息不一样,如果中间发生一次Full GC,那么前面的list集合中的软引用对象全为null。

5. 虚引用(Phantom Reference)

虚引用是最弱的引用类型,它不会阻止对象被垃圾回收。虚引用的主要作用是允许您在对象被回收时收到通知。通常与引用队列(Reference Queue)一起使用,可以在对象被回收时执行一些清理或其他操作。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/196952.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!