Two aspects of the Internet suppression cloud
Although it is difficult to be strange on the two sides of the Internet and cloud suppression, you can indeed learn a lot after reviewing it.
I also thank many bigwigs for sharing their learning ideas and thinking directions with me, so I wrote this article. Please update it later.
Must read before reading:
If you just want to know the answer to this question, please go out and turn left to find someone else's blog. I did not release the answer here, but provided my own ideas with "how to think about the question itself."
So now let's get back to the topic.
Disadvantages of using reference counting in Java GC
First of all, it is well known to JavaerCircular dependency,Then what?
To be honest, during the interview, I was a little panicked because of the first time I met [I didn’t introduce myself and I went straight to the topic without introducing myself. I just said a sentence and was promptly told to the next question]. After all, the interview experience is not enough. Of course, this does not prevent me from thinking of answers, but my mentality has indeed been affected and needs to be adjusted in time.
Then, when there is no clear answer, it shouldHow to think about this question? From what aspects do you think?
To put it bluntly, you have to decorate something in your blank brain.
If you are just starting out, you can say it roughly. If you have clear ideas, you can talk about it in a structured manner.
Quote something butter to me (this is simply a guiding light on my journey to job hunting)
For the current Internet, it is actually just pursuing two things, one is moreSafety, one is morefast, on these two basiss, we are pursuing boundary and connection between each other.
So it is actually very easy to find that when we learn something, their designers are simply designing and optimizing around these two points.
——Double non-sense also has dreams
So next, we will go fromSafetyandspeedTo say.
First, "safe".
(To quote him, because it's too incisive
Any technical operation is nothing more than a foothold forMemoryThis is also the reason why we have to work hard to study the operating system and computer basics. The operation of memory is actually the operation of data.
——Double non-sense also has dreams
So we can think further:The relationship between citation counting and "memory"。
So, do we encounter memory-related problems in our daily programming?
some.Memory leak,Memory overflow. (This is also a common knowledge point in interviews, memory overflow andMemory leakThe difference.
We know that the implementation of reference counting is nothing more than installing a counter on the object. Whenever an additional object refers to the object, or a reference to the object is discarded, the counter needs to be modified.
So is this implementation related to memory problems?
Why not think about it first
What is a memory leak?
——Memory Leak, cannot free up the memory space that has been applied for.
What is memory overflow?
——OOM, when allocating memory to objects, there is not enough memory.
So,Will reference counting lead to memory leaks?
In detail, heWill the requested memory space cannot be released?
If you don't have any questions, then change your question.Will the object not be recycled?That is,Will there be an object's reference count never be 0?
Now we should suddenly realize that the problem of our "everyone knows" circular dependency is nothing more than the object's reference count will never decrease by less than 0.
This actually brings about "security" problems. To put it more specifically, it is the problem of "memory leak".
(Although it seems that I am trying to retrace the answer but I am trying to retrace the answer. 😂
Let's take a look now,Will reference counting cause memory overflow?
Will this cause insufficient memory when allocating memory to objects?
It seems not, of course you can also say: Doesn’t installing a counter for each object take up a lot of memory?
I think you can tell the interviewer because I have this idea too. 🤣
Now, it seems that the "safety" aspect has been considered almost done?
(My ideas here are mainly based on "memory issues", which can broaden my own ideas, because I am not at a high level, so I can only think of these.
Next, let’s try to take the “speed” path.
We know that the function of the reference counting method itself is to find objects that can be recycled. Then the algorithm for judging the recyclable object will have the requirement of "speed".
Obviously, the reference counting methodjudgeWhether the object is recyclable is enough to compare it with 0, which is very fast, which is also its biggest advantage.
Then why do I bold the "judgment"?
Think about it,In addition to judging whether it is recyclable quickly, what other speed should GC consider?
If you don't have any ideas, think about it,Is "reference" a matter between an object?
Obviously, when A quotes B, it must be a matter involving both parties.
Suppose now that A's reference to B's has expired, what does B need to do?renew。
Have you thought of it?
We not only requirejudgeBe quickrenewIt can't be slow either.
Suppose there is now a chain reference, A refers to B, B refers to C,…, Y refers to Z. Now the source of evil A discards the reference to B, or A is recycled.
What will happen next?Updates of chain effects, and recycling.
What if it is not chained, but mesh?Flooding effect updates, and recycling.
In actual projects, the reference chain of objects is usually very complicated, and the failure of any reference may trigger the update of the counter of the entire object graph (I personally think it is a bit similar to the routing algorithm ospf).
Now assume that it is highconcurrentIn the case of , the references of many objects change, what problems will bring? What problems should be solved? How to solve it?
I personally think that these are actually the problems that need to be solved if jvm uses the reference counting method (a small part of them).
How would you solve the open idea?
In fact, this is just a way of thinking if I asked me to rethink this issue. Of course, during the formal interview, I have to see if my mind is empty or not.
In fact, you can think a lot, and you can compare and find shortcomings with accessibility analysis, etc. The ways of thinking are diverse.
In addition, if there are any errors in the middle, please point them out and learn together, thank you.