"What's the difference between a strong reference, a soft reference, a weak reference, and a phantom reference?"

This problem stumped many senior Java engineers, not because of how difficult the problem itself is.

But it is indeed a relatively niche knowledge point.

Hi, my name is Mic, a Java programmer who has been working for 14 years.

Today I will share with you the standard answer to this interview question.

I have compiled the text version into a 15W word interview document, you can send me a private message to get it

Check out the experts' answers below.

Expert:

Different reference types mainly reflect the different reachability states of objects and their impact on garbage collection.

A strong reference is a reference to an ordinary object. As long as there is a strong reference pointing to an object, it means that the object is still "alive", and the garbage collector cannot reclaim this type of object.

Only when there is no other reference relationship, or exceeds the scope of the reference, or explicitly assigns the reference to null, the garbage collector can reclaim the memory.

A soft reference is a relatively weak reference that makes the object exempt from some garbage collection. Only when the JVM thinks that the memory is insufficient, it will try to reclaim the object pointed to by the soft reference.

Soft references are usually used to implement memory-sensitive caches. If there is free memory, the cache can be temporarily reserved and cleaned up when the memory is insufficient, so as to ensure that the cache will not be exhausted while using the cache.

Weak references, relative to strong references, allow objects that are garbage collected in the presence of reference associations. During the process of the garbage collector thread scanning the memory area under its jurisdiction, once an object with only weak references is found, no matter Whether the current memory space is sufficient or not , the memory virtual reference will be reclaimed during the garbage collection period. It does not determine the life cycle of the object. It provides a mechanism to ensure that the object can do certain things after it is finalized.

When the garbage collector is ready to reclaim an object, if it finds that it still has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the memory of the object.

The program can know whether the referenced object will be garbage collected by judging whether a virtual reference has been added to the reference queue , and then we can take necessary actions before the memory of the referenced object is reclaimed.

Summarize

This is a good question. It involves the knowledge points as a whole, if you want to dig deeper.

You can also expand the reachability status analysis of objects and the recovery principle of GC.

However, it is indeed a relatively partial problem, and it will be more applied in some class libraries or frameworks.

Interested partners can follow up with further in-depth research.

Remember to like, subscribe and follow.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。