zoukankan      html  css  js  c++  java
  • java中的引用类型

    介绍

    java中的引用有4种类型:强引用(Strong Reference),软引用(Soft Reference),弱引用(Weak Reference),虚引用(Phantom Reference),强度依次减弱。

    前置准备

    配置JVM参数,-Xms10M -Xmx20M,初始内存10M,最大内存20M,以IDEA为例

    强引用

    java默认声明的就是强引用,只要强引用还存在,被引用的对象就不会被回收,内存不足时,JVM会直接抛出OutOfMemoryError

    public class Client {
    
      public static void main(String[] args) {
        byte[] bytes = new byte[10 * 1024 * 1024];
        System.out.println(bytes);
      }
    
    }
    

    我们申请10M的内存空间,可以正常运行。

    public class Client {
    
      public static void main(String[] args) {
        byte[] bytes = new byte[20 * 1024 * 1024];
        System.out.println(bytes);
      }
    
    }
    

    申请20M,就会抛出错误

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    

    软引用

    在内存充足的时候,软引用不会被回收,内存不足,JVM即将抛出OOM时,软引用就会被回收。这种特性可以用来实现缓存技术。

    public class Client {
    
      public static void main(String[] args) {
        List<byte[]> bytes = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
          bytes.add(new byte[10 * 1024 * 1024]);
        }
        for (byte[] aByte : bytes) {
          System.out.println(aByte);
        }
      }
    
    }
    

    一共申请了50M内存,抛出错误

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    
    public class Client2 {
    
      public static void main(String[] args) {
        List<SoftReference<byte[]>> bytes = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
          bytes.add(new SoftReference<>(new byte[10 * 1024 * 1024]));
        }
        for (SoftReference<byte[]> reference : bytes) {
          System.out.println(reference.get());
        }
      }
    
    }
    

    使用SoftReference类创建软引用,执行结果为

    null
    null
    null
    null
    [B@3c09711b
    

    这样就说明了在内存不足的情况下,软引用会被自动回收。上面的情况是软引用所指向的对象已经没有强引用了,如果还有强引用,那么软引用不会被回收

    public class Client2 {
    
      public static void main(String[] args) {
        List<SoftReference<byte[]>> softReferences = new ArrayList<>();
        List<byte[]> bytes = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
          byte[] arr = new byte[10 * 1024 * 1024];
          bytes.add(arr);
          softReferences.add(new SoftReference<>(arr));
        }
        for (SoftReference<byte[]> reference : softReferences) {
          System.out.println(reference.get());
        }
      }
    
    }
    

    还是会抛出OOM

    弱引用

    不管内存是否充足,只要JVM开始垃圾回收,弱引用所指向的对象就会被回收。

    public class Client2 {
    
      public static void main(String[] args) {
        List<SoftReference<byte[]>> softReferences = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
          byte[] arr = new byte[10 * 1024 * 1024];
          softReferences.add(new SoftReference<>(arr));
        }
        System.gc();
        for (SoftReference<byte[]> reference : softReferences) {
          System.out.println(reference.get());
        }
      }
    
    }
    

    输出结果为

    null
    null
    null
    null
    [B@3c09711b
    

    对比软引用,我们看一下弱引用

    public class Client {
    
      public static void main(String[] args) {
        List<WeakReference<byte[]>> softReferences = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
          byte[] arr = new byte[10 * 1024 * 1024];
          softReferences.add(new WeakReference<>(arr));
        }
        System.gc();
        for (WeakReference<byte[]> reference : softReferences) {
          System.out.println(reference.get());
        }
      }
    
    }
    

    输出结果为

    null
    null
    null
    null
    null
    

    对象全都被垃圾回收了。

    虚引用

    虚引用是最弱的一种引用关系,相当于无引用,必须与引用队列一起使用

    public class Client {
    
      public static void main(String[] args) {
        ReferenceQueue<byte[]> referenceQueue = new ReferenceQueue<>();
        PhantomReference<byte[]> phantomReference = new PhantomReference<byte[]>(
            new byte[10 * 1024 * 1024], referenceQueue);
        System.gc();
        System.out.println(referenceQueue.poll() == phantomReference);
      }
    
    }
    

    垃圾回收之后,虚引用将被放入引用队列中。

  • 相关阅读:
    mvc:resources配置说明
    MySQL 表与索引损坏修复
    ORACLE 日志损坏 使用"_ALLOW_RESETLOGS_CORRUPTION"进行崩溃恢复
    Oracle 回滚段坏快并恢复
    Oracle 坏快处理:Undo 与 datafile
    Oracle备份恢复-控制文件损坏的各种场景恢复专题
    Oracle备份恢复-redo文件损坏的各种场景恢复专题
    Oracle 数据库坏块处理技术
    PostgreSQL 坏快分类与修复策略
    Linux RAID卡优化
  • 原文地址:https://www.cnblogs.com/strongmore/p/13910955.html
Copyright © 2011-2022 走看看