Do you understand the strengths and weaknesses in Java?

In order to make full use of the computer's memory, objects are designed to have a life cycle. When the object's life cycle ends, it will be recycled by the garbage collector, thereby releasing memory resources. In order to consider various scenarios of recycling, four reference types, strong, soft, weak, and virtual, are cited in JDK1.2.

Strong citation

If an object is a strong reference, then the garbage collector will not easily reclaim it. Only when the object is not used, the garbage collector will reclaim it.

Create one new StrongReference(), use a reference strongReferenceto point to the address of this memory space, when the reference is set null, the garbage collector will start the finalize()method. Let's look at the results of the operation.

StrongReference类被回收了!!!

If this object has been referenced, even if the memory is full, it will not be garbage collected. Would rather throw OutOfMemoryError.

However, if you are not proficient in memory recovery, never use finalize()methods.

finalize()The role is often considered to be used for the final resource recovery. This method has a lot of uncertainty (it does not guarantee that the tasks in the method are executed) and the running cost is high. Therefore, it will not be good for recycling resources.

There is another stalk here. Many of the millet engineers back then switched from C++ to Java. C++ generally needs to clean up the memory manually, so I am used to rewriting finalize(). But the system always has various memory problems. The method of garbage removal is the JVM.

Soft reference

When the system memory is insufficient, GC will be triggered. After garbage collection, the memory is still insufficient, and the objects wrapped by soft references will be cleaned up.

Let's test test1(). Test Results:

Student(grade=null)

The object here has not been recycled, so it can be obtained Student.

Let's test it again test2(). In order to simulate the real environment, the memory is adjusted to 20MB here.

operation result

[B@7960847b
[B@7960847b
null

When two byte arrays are created, the memory is full, GC Studentis triggered, and the object is recycled.

So what scenarios can soft references be used for?

Soft references can be used to implement memory-sensitive caches. When the system space is insufficient, cached objects can be cleaned up without affecting business.

Weak reference

If GC is triggered, the memory is sufficient at this time, but the weakly referenced objects will still be recycled. Therefore, objects associated with weak references can only survive until the next garbage collection occurs.

operation result

before gc: r=I'm here, static=I'm here
after gc: r=null, static=I'm here

If it is a WeakReferencewrapped object, then this object is weakly referenced. After we see the GC r.get=null. But sr.get()there is value. If the wrapped data is static, even if it is weakly referenced, it will not be recycled by the GC.

In fact ThreadLocal, weak references are also used. For details, you can take a look at my previous article:

Use ThreadLocal afraid memory leak? Then you should read this article

Phantom reference

This is how phantom references have several characteristics:

  • It is not possible to obtain a real reference to an object through a phantom reference.

  • Phantom references must ReferenceQueuebe used together. When the GC prepares to reclaim an object, if it finds that it still has a phantom reference, it will add the phantom reference to the associated one before the collection ReferenceQueue.

operation result:

null

So what is the use of setting an object as a phantom reference?

The only purpose of setting a phantom reference association for an object is to receive a system notification when the object is reclaimed by the garbage collector.

There will also be virtual references to manage off-heap memory.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

 

Guess you like

Origin blog.csdn.net/wujialv/article/details/108617934