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

    你可能感兴趣的文章
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>