编程知识 cdmana.com

Four rounds of interviews a week finally won the offer and started java development quickly

GC summary

garbage collection (Garbage Collection) Usually called “GC”, By virtual machine “ automation ” Complete garbage collection .

Think about a problem , since GC Will automatically recycle , Why should developers learn GC And memory allocation ? In order to be able to configure the above parameter configuration ? What is the purpose of parameter configuration ?

When you need to check all kinds of memory overflows , When memory leaks , When garbage becomes a bottleneck for the system to achieve higher concurrency , We need to be right GC Automatic recovery of the implementation of the necessary monitoring and regulation .

JVM Middle program counter 、 Virtual machine stack 、 Native Method Stack 3 Regions are created and destroyed by threads . Stack frame with the method into and out of the stack to do the operation , Realize automatic memory cleaning . Their memory allocation and recycling are deterministic .

therefore ,GC Garbage collection is mainly concentrated in the heap and method area , During program operation , This part of memory is allocated and used dynamically .

Next, we will understand the concept and specific algorithm GC The process of recycling .

How to judge the survival of an object

There are two ways of judging objects : Reference counting algorithm and reachability analysis algorithm (Reachability Analysis).

Reference counting algorithm : Adds a reference counter to the object , Every time a place references it, the counter adds 1, Count minus when reference is released 1, When the counter is 0 It can be recycled .

The implementation of reference counting algorithm is simple , Judge efficiently , At Microsoft COM and Python Language is widely used , But in the mainstream Java This method is not used in virtual machines , The main reason is that it can't solve the problem of circular reference between objects .

Reachability analysis algorithm : The basic idea is through a series called “GC Root” The object of ( Such as system class loader 、 Objects in stack 、 Active threads, etc ) As a starting point , Based on the object reference relationship , Start looking down , The path taken is called the reference chain , When an object arrives GC Root There's no chain of references connected , The object of proof is not available .

 A week  4 Won the first round of interview offer,Java Develop quickly _Java

The green part in the picture above is the living object , The gray part is recyclable object . Although the gray part is still related inside , But they have to GC Root It's not reachable .

Interview questions

interviewer , say something Java GC What algorithms are used ? Where to apply them respectively ?

answer : Copy algorithm 、 Mark clear 、 Tag to sort out ……

Are you still learning by rote ? Keep looking down , You'll suddenly open up , No more rote learning .

Tag clearing algorithm

Mark clear (Mark-Sweep) Algorithm , contain “ Mark ” and “ eliminate ” Two phases : First mark all objects to be recycled , Recycle all marked objects after marking .

Mark clearing algorithm is the most basic collection algorithm , Subsequent collection algorithms are based on this idea and improve its shortcomings .

 A week  4 Won the first round of interview offer,Java Develop quickly _ The programmer _02

Main drawback : One is efficiency , The efficiency of the marking and removal process is not high ; The other is space , After the tag is cleared, a large number of discontinuous memory fragments will be generated , Too much space debris can lead to , When the program needs to allocate large objects in the future, it cannot find enough continuous memory and has to trigger another garbage collection action in advance .

Copy algorithm

Copy (Copying) Algorithm : Divide the available memory into two equal sized blocks according to capacity , Use only one piece at a time . When a piece of memory runs out , Copy the living object to another piece , Then clean up the previous piece .

 A week  4 Won the first round of interview offer,Java Develop quickly _ The programmer _03

Every time half area memory is recycled 、 When allocating memory, we don't need to consider the complexity of memory fragmentation , Just move the top pointer , Allocate memory in order , Implement a simple , Efficient operation .

shortcoming : Reduce memory by half , Low cost performance , Continuous replication of long-lived objects leads to inefficiency .

JVM The new generation in the heap adopts the replication algorithm . Go back to the original push distribution structure .

 A week  4 Won the first round of interview offer,Java Develop quickly _ The programmer _04

stay GC In the process of recycling , When Eden Area is full , Objects that are still alive will be copied to one of Survivor District ; When recycling , Will Eden And used Survivor What's still alive in the area , Copy to another Survivor District , Then on Eden And used Survivor Clean up the area .

If the other Survivor When there is not enough memory in the area , They will enter the old age .

There is such a mechanism for which objects will enter the elderly generation : Every time an object undergoes a replication , Age plus 1, After reaching the age threshold for promotion , Move to the old days .

In the whole process , because Eden The objects in are like duckweeds “ Instant birth, instant extinction ” The object of , So there's no need for 1:1 To allocate memory , Instead, it adopted 8:1:1 To distribute .

And for those like “ Water bearworm ” equally , After many times of cleaning up the living objects , They will enter the old age , And the cleaning algorithm for the elderly uses the following “ Marking algorithm ”.

summary

On the whole , If you want to change your career to work as a programmer ,Java Development must be your first choice . But no matter what programming language you choose , Improving your hardware strength is the only way to get a high salary .

If you follow this learning route , You will have a more systematic knowledge network , It's not like learning knowledge in a scattered way . Personally, I don't recommend watching... At the beginning 《Java Programming idea 》、《Java The core technology 》 These books , After reading it, you will definitely give up studying . It is suggested that you can watch some videos to learn , It's very rewarding to buy these books when you can get started .

 A week  4 Won the first round of interview offer,Java Develop quickly _ The programmer _05

This article has been  CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included

版权声明
本文为[Programmer Xia ya]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002002410507g.html

Scroll to Top