3

Hi, my name is Mic.

Today I will share some interview questions that must be asked by first-tier Internet companies.

"How does the JVM determine that an object can be recycled"

On this question, let's take a look at the answers of ordinary people and experts.

Ordinary people:

Um. . . . . . . . . .

Expert:

OK, interviewer.

In the JVM, to determine whether an object can be recycled, the most important thing is to determine whether the object is still being used, and only objects that are not used can be recycled.

  1. Reference counter, that is, adding a reference counter to each object to count the number of references to the current object,

    If there is an update applied to the current object, the reference counter is incremented. Once the reference counter becomes 0, it means that it can be recycled.

    This method requires extra space to store the reference counter, but its implementation is simple and efficient.

    image-20220423173852373

    However, mainstream JVMs do not use this method, because the reference counter is used to deal with some complex circular references or mutual dependencies.

    There may be some memory that is no longer used but cannot be reclaimed, causing memory leaks.

    image-20220423170255641

  2. Reachability analysis, its main idea is to first determine a series of objects that must not be recovered as GC root,

    For example, the reference objects in the virtual machine stack, the objects referenced by the local method stack, etc., and then use GC ROOT as the starting node,

    Search down from these nodes to find its directly and indirectly referenced objects. After traversing, if some objects are found unreachable,

    Then it is considered that these objects are useless and need to be recycled.

    During garbage collection, the JVM will first find all GC roots, and this process will suspend all user threads.

    That is, stop the world, and then search down from the root nodes of GC Roots. Reachable objects are retained, and unreachable objects are recycled.

    Reachability analysis is an algorithm currently used by mainstream JVMs.

    The above is my understanding of the problem.

    image-20220423172025944

Summarize

Many fans told me that many things were forgotten after a while, and asked me how I remembered them.

I told him that there is no need to memorize such things as technology. The only thing you can do is to reduce fragmented learning and spend more time on systematic learning. Only systematic knowledge will not be forgotten.

However, the process of building systematic knowledge is more than ten thousand times more painful than fragmented point-based learning.

There is no shortcut to the precipitation of technology, you can only spend hard work to learn.

Friends who like my works remember to like and favorite.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Reprint please indicate the source 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年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。