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.
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.
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.
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.
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.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Reprint please indicate the sourceMic带你学架构
!
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!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。