c java 引用类型_java中的引用类型

介绍

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

前置准备

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

5a2ebc22b3c020da18881d0c68f9579e.png

强引用

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 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> bytes = new ArrayList<>();

for (int i = 0; i < 5; i++) {

bytes.add(new SoftReference<>(new byte[10 * 1024 * 1024]));

}

for (SoftReference reference : bytes) {

System.out.println(reference.get());

}

}

}

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

null

null

null

null

[B@3c09711b

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

public class Client2 {

public static void main(String[] args) {

List> softReferences = new ArrayList<>();

List 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 reference : softReferences) {

System.out.println(reference.get());

}

}

}

还是会抛出OOM

弱引用

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

public class Client2 {

public static void main(String[] args) {

List> 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 reference : softReferences) {

System.out.println(reference.get());

}

}

}

输出结果为

null

null

null

null

[B@3c09711b

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

public class Client {

public static void main(String[] args) {

List> 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 reference : softReferences) {

System.out.println(reference.get());

}

}

}

输出结果为

null

null

null

null

null

对象全都被垃圾回收了。

虚引用

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

public class Client {

public static void main(String[] args) {

ReferenceQueue referenceQueue = new ReferenceQueue<>();

PhantomReference phantomReference = new PhantomReference(

new byte[10 * 1024 * 1024], referenceQueue);

System.gc();

System.out.println(referenceQueue.poll() == phantomReference);

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值