Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of weak references in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章主要讲解了"Java中的弱引用有什么用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java中的弱引用有什么用"吧!

Java里一个对象obj被创建时,被放在堆里。当GC运行的时候,发现没有任何引用指向obj,那么就会回收obj对象的堆内存空间。

换句话说,一个对象被回收, 必须满足两个条件:

(1)没有任何引用指向它

(2)GC被运行。

在实际开发中,我们可以通过把所有指向某个对象的referece置空来保证这个对象在下次GC运行的时候被回收,类似下面:

Object c = new Car(); c=null;

但是,这样做是一件很繁琐并且违背GC自动回收原则的事。对于简单的情况, 手动置空是不需要程序员来做的, 因为在java中, 对于简单对象, 当调用它的方法执行完毕后, 指向它的引用会被从栈中弹出, 所以它就能在下一次GC执行时被回收了。

但是, 也有特殊例外. 当使用cache的时候, 由于cache的对象正是程序运行需要的, 那么只要程序正在运行, cache中的引用就不会被GC(或者说, cache中的reference拥有了和主程序一样的life cycle). 那么随着cache中的reference越来越多, GC无法回收的object也越来越多, 无法被自动回收。当这些object需要被回收时, 回收这些object的任务只有交给程序编写者了。然而这却违背了GC的本质(自动回收可以回收的objects)。

所以, java中引入了weak reference。

Object c = new Car(); //只要c还指向car object, car object就不会被回收 -->(强引用)

当一个对象仅仅被weak reference指向, 而没有任何其他strong reference指向的时候, 如果GC运行, 那么这个对象就会被回收。另外,关注公众号Java技术栈,在后台回复:面试,可以获取我整理的 JVM 系列面试题和答案,非常齐全。

下面这个是网上的例子,首先定义一个实体类:

public class Car { private double price; private String color; public Car(double price, String color) { this.price = price; this.color = color; } public double getPrice() { return price; } public String getColor() { return color; } public String toString() { return "This car is a " + this.color + " car, costs $" + price; } }

一般使用WeakReference的时候都会定义一个类继承自WeakReference,在这个类中再定义一些别的属性,这里就不定义别的属性了:

public class WeakReferenceCar extends WeakReference { public WeakReferenceCar(Car car) { super(car); } }

main函数调用一下,当然为了更清楚地看到GC的效果,设置虚拟机参数"-XX:+PrintGCDetails":

public static void main(String[] args) { Car car = new Car(2000.0, "red"); WeakReferenceCar wrc = new WeakReferenceCar(car); wrc.setStr("111"); int i = 0; while (true) { if (wrc.get() != null) { i++; System.out.println("WeakReferenceCar's Car is alive for " + i + ", loop - " + wrc); } else { System.out.println("WeakReferenceCar's Car has bean collected"); break; } } }

最后是运行结果:

WeakReferenceCar's Car is alive for 68450, loop - interview.WeakReferenceCar@776ec8df WeakReferenceCar's Car is alive for 68451, loop - interview.WeakReferenceCar@776ec8df WeakReferenceCar's Car is alive for 68452, loop - interview.WeakReferenceCar@776ec8df WeakReferenceCar's Car is alive for 68453, loop - interview.WeakReferenceCar@776ec8df [GC (Allocation Failure) [PSYoungGen: 34304K->1000K(38400K)] 34320K->1016K(125952K), 0.0015129 secs] [Times: user=0.02 sys=0.02, real=0.00 secs] WeakReferenceCar's Car is alive for 68454, loop - interview.WeakReferenceCar@776ec8df WeakReferenceCar's Car has bean collected Heap PSYoungGen total 38400K, used 1986K [0x00000000d5e00000, 0x00000000da900000, 0x0000000100000000) eden space 33280K, 2% used [0x00000000d5e00000,0x00000000d5ef6b70,0x00000000d7e80000) from space 5120K, 19% used [0x00000000d7e80000,0x00000000d7f7a020,0x00000000d8380000) to space 5120K, 0% used [0x00000000da400000,0x00000000da400000,0x00000000da900000) ParOldGen total 87552K, used 16K [0x0000000081a00000, 0x0000000086f80000, 0x00000000d5e00000) object space 87552K, 0% used [0x0000000081a00000,0x0000000081a04000,0x0000000086f80000) Metaspace used 3547K, capacity 4564K, committed 4864K, reserved 1056768K class space used 381K, capacity 388K, committed 512K, reserved 1048576K

可以看到在68454循环之后,WeakReferenceCar关联的对象Car被回收掉了,注意是弱引用关联的对象car被回收,而不是弱引用本身wrc被回收。

WeakReference的一个特点是它何时被回收是不可确定的, 因为这是由GC运行的不确定性所确定的. 所以, 一般用weak reference引用的对象是有价值被cache, 而且很容易被重新被构建, 且很消耗内存的对象.

在weak reference指向的对象被回收后, weak reference本身其实也就没有用了. java提供了一个ReferenceQueue来保存这些所指向的对象已经被回收的reference. 用法是在定义WeakReference的时候将一个ReferenceQueue的对象作为参数传入构造函数。

感谢各位的阅读,以上就是"Java中的弱引用有什么用"的内容了,经过本文的学习后,相信大家对Java中的弱引用有什么用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report