博客
关于我
JavaHigh04-强引用、弱引用、软引用、虚引用
阅读量:171 次
发布时间:2019-02-27

本文共 3527 字,大约阅读时间需要 11 分钟。

Java引用类型详解:强引用、软引用、弱引用与虚引用

在Java编程中,引用类型是对象垃圾回收(GC)和内存管理的重要概念。不同引用类型在内存管理和垃圾回收策略中具有不同的特点和用途。本文将从强引用到虚引用的四种引用类型逐一探讨其特性及其应用场景。


一、强引用

强引用是最常见的引用类型。在GC过程中,只要对象与强引用关联,JVM就不会将其回收。即使内存不足也会抛出OOM(OutOfMemory)错误,但对象依然不会被回收。

示例代码:

String str1 = new String("aa");String str2 = str1;str1 = null;System.gc();System.out.println(str1); // 输出nullSystem.out.println(str2); // 输出"a a"

这种特性使得强引用适用于大多数普通对象的存储和使用场景。


二、软引用

软引用的特点是:内存足够时不会回收,内存不足时会被回收。软引用适合用于缓存场景,尤其是在内存有限时。

内存充足时的示例:

public void softRef() {    Object obj1 = new Object();    SoftReference softReference = new SoftReference<>(obj1);    System.out.println(obj1); // 输出"对象对象"    System.out.println(softReference.get()); // 输出"对象对象"    obj1 = null;    System.gc();    System.out.println(obj1); // 输出null    System.out.println(softReference.get()); // 输出null}

内存不足时的示例:

public void softRefNotRnoughMemory() {    Object obj1 = new Object();    SoftReference softReference = new SoftReference<>(obj1);    System.out.println(obj1); // 输出"对象对象"    System.out.println(softReference.get()); // 输出"对象对象"    obj1 = null;    System.out.println(obj1); // 输出null    System.out.println(softReference.get()); // 输出null}

为了测试软引用的行为,可以通过配置较小的内存(如-XX:MaxHeapSize=5m)来触发内存不足的情况。


三、弱引用

弱引用在GC时,无论内存是否充足,都会被回收。弱引用通常用于缓存中的对象,确保内存释放时优化性能。

示例代码:

public void weakRef() {    Object obj1 = new Object();    WeakReference weakReference = new WeakReference<>(obj1);    System.out.println(obj1); // 输出"对象对象"    System.out.println(weakReference.get()); // 输出"对象对象"    obj1 = null;    System.gc();    System.out.println("--------------------");    System.out.println(obj1); // 输出null    System.out.println(weakReference.get()); // 输出null}

弱引用适用于需要在GC时自动释放内存的场景。


四、虚引用

虚引用与弱引用不同,在GC时不一定会被回收,并且不能通过虚引用访问对象。虚引用通常与ReferenceQueue配合使用,以在对象被回收前执行某些操作。

示例代码:

public void virtualRef() throws InterruptedException {    Object obj1 = new Object();    ReferenceQueue referenceQueue = new ReferenceQueue<>();    WeakReference weakReference = new WeakReference<>(obj1, referenceQueue);    System.out.println(obj1); // 输出"对象对象"    System.out.println(weakReference); // 输出"弱引用"    System.out.println(referenceQueue.poll()); // 输出null    System.out.println("-------------------");    obj1 = null;    System.gc();    Thread.sleep(500);    System.out.println(obj1); // 输出null    System.out.println(weakReference.get()); // 输出null    System.out.println(referenceQueue.poll()); // 输出非null}

虚引用适用于需要在对象被回收前执行特定操作的场景。


引用类型的实际应用

在实际开发中,引用类型的选择取决于内存管理和性能优化需求。例如:

  • 图片缓存:使用软引用缓存本地图片,避免内存溢出。
  • 对象持久化:使用弱引用确保对象在GC时被自动释放。
  • ReferenceQueue:结合虚引用和ReferenceQueue实现对象回收时的定制处理。

  • 弱引用与HashMap的区别

    • 普通HashMap:即使对象被回收,也会影响HashMap的性能和内存使用。
    • WeakHashMap:当对象被回收时,HashMap中的entry会被自动移除,不影响内存管理。

    示例代码:

    public void myHashMap() {    HashMap
    map = new HashMap<>(); Integer key = new Integer(1); String value = "HashMap"; map.put(key, value); System.out.println(map); // 输出键值对 key = null; System.out.println(map); // 输出键值对 System.gc(); System.out.println(map + "\t" + map.size()); // 输出键值对和大小}

    示例代码:

    public void myWeakHashMap() {    WeakHashMap
    map = new WeakHashMap<>(); Integer key = new Integer(1); String value = "HashMap"; map.put(key, value); System.out.println(map); // 输出键值对 key = null; System.out.println(map); // 输出键值对 System.gc(); System.out.println(map + "\t" + map.size()); // 输出键值对和大小}

    通过WeakHashMap可以有效地管理内存,避免内存泄漏问题。


    虚引用的实际应用

    虚引用通常用于实现对象的生命周期管理。例如:

    • 在某些框架中,虚引用可以用来监控对象的状态,并在对象被回收时触发特定逻辑。
    • 在垃圾回收过程中,虚引用可以与ReferenceQueue配合使用,确保回收的对象能够被正确处理。

    通过对强引用、软引用、弱引用和虚引用的理解,可以更好地优化Java程序的内存管理和性能表现。

    转载地址:http://cgmb.baihongyu.com/

    你可能感兴趣的文章
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>