博客
关于我
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/

    你可能感兴趣的文章
    Vue3+element plus+sortablejs实现table列表拖拽
    查看>>
    Nokia5233手机和我装的几个symbian V5手机软件
    查看>>
    non linear processor
    查看>>
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    nopcommerce商城系统--文档整理
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>