Story: JVM into the world (with illustrations)

JVM into the world

  1. note! This article is longer, it is recommended to collection and re-read. More articles can be of public concern number: the actual code
  2. You can also star your warehouse article on my GitHub: https://github.com/flyhero/MarkNote
  3. Description: This concept in Java 8 Hotspot 64-bit operating system

Xiao Qiang is a job for more than three years of development engineers, from his hair the amount you can know, Johnny qualifications also still shallow.

The amount of fat

Recently the company nothing, he began bored of it. That afternoon, my colleagues in the discussion of this highly business, but he did not participate, so he decided to learn what knowledge, boring turned each page, find JVM is the great God who recommended the knowledge and decided to take a good look .

Five minutes later ......

Xiaoqiang feel that this knowledge is boring, no wonder that the great God who can see! Looked a few minutes, Johnny weariness hit, rubbed his eyes bleary-eyed.

However, at this moment, he suddenly realized that the voice of intense discussion surrounding colleagues do not hear the quiet to the extreme.

1. into the community

Xiaoqiang efforts to open their eyes, only to find that he was living in a vast expanse of space, scared to jump, I thought this is how, crossed? But also across the world get through to a picturesque, beautiful clouds ah ...... place, this situation ......
JVM illusion

Suddenly came in front of a white-bearded old man, Johnny was going to open, pre-empted the old man: Hello, I am the founder of the world's JVM, you can call me "HotSpot", but that does not matter, because I created this world, According to "JVM norms" to complete. I am taking a break, to find a guest, it's you.

Xiaoqiang: I would like to know ......

Old man: Do not ask, I know, you want to know what I have created this world! Come with me.

This old man, I did not speak, which is over! Well, look at you now say then.

The old man walked Road: JVM world space is limited, we adhere to a principle: carry out their duties, leaving useless man!

Xiaoqiang: ah! Good cruel.

Old man: No, it's not cruel, we are born in this world to serve customers, for customers to shine, everyone devote their abilities is the successful completion of the task, exit the stage is as it should be, it is their best destination.

Xiaoqiang: Also, I learned programming is the truth ah, do not enlighten me how so ......

2. Layout

Come with you to see how our overall composition and layout of the central area of ​​the world.
overall arrangement

Let's look at our most important daily work zone (run-time data area), in order for us to work together more efficiently, we will be the world's space is divided into several sections.

Residential area - heap

Here is a residential area outside the way people work, live area of ​​our people based on age further separation of the Eden District, District survivors, aged area.
Residential area

Workspace - Stack

Each task comes, it will open up in a separate work area for a place to accomplish this task.
Stack frame

Recorder - the program counter

We also do limited tasks, so we need to be divided into different tasks different time slots, when we switch tasks, requiring a recorder capable of recording where we did this task, we come back next time to continue to do .

Warehouse Management District - District method

Here kept the workers and the common template of the same tools.

3. Life and Death

The people working here will experience life and death, the majority of people live to see old age, but it is not important that he contributed to us.

3.1 Birth

Old man: Everyone here has a template (class), saw the rest are residential tall it? He called Zhang Third, he is given in accordance with external customers to create a template of "User Class", he's a customer favorite workers. You know the customers of these templates (class) is how to enter into our world do?

Xiaoqiang: I know this point before seen a little bit. This process is a little complicated, customers template (class) is translated by a factory (compiler) will translate it into class bytecode, because you only know this world bytecode, and then have your loading system load them to here.

The loading process of these stages:
load

Wherein the loading by the loader stage is done.

Old man: Yes, we offer three factories, start class loader, the extension class loader, the application class loader, of course, customers can also custom loader.
Delegate parents

Xiaoqiang: they follow the parent delegation model, but I have not quite understand the word!

Old man: This is due to problems of language translation of your cause, this mode is called "parents delegation", you know it! It means to have your fathers to help you complete.

Xiaoqiang: Delegate mode that parents what good is it?

The old man:

  1. Relations template can be avoided with a priority level (class) of repeated loading
  2. Security considerations may prevent Java core api to be replaced

The old man continued: three-step process that connection, what do you know it?

Xiaoqiang: I do not know the specific hey ......

The old man smiled: For customer-defined template (class), we are not accept all comers, for safety and to provide better service, we have the template of our world do some validation and subsequent operations.

Verification includes verification format, metadata validation, bytecode verification, validation symbols. Once verified, we will rely on something as a template (class variable) allocation of space, and finally replaced by a direct reference symbol references.

The old man looked at Johnny frowned, then went on to add: you may not know what is symbolic references and direct reference!

Is a symbolic reference at compile time, not knowing other things template (class) depends, which will be our position in space, it can only be represented by a symbol.

There is a direct reference to your real address space is loaded into everything here, and then to replace the symbolic references. You will be able to find something they depend on such operation.

Last is initialized, this stage is mainly a class variable initialization, is the process of implementation of the class constructor.

Xiaoqiang: I do not see how these templates it?

Old man: These templates I put them hide behind the world, most people did not see, they collectively known as Klass.

Xiaoqiang: No, ah! You made a mistake? It should not be called Class?

The old man: Ha ha! I just said, most people did not see, you're one of them ah! You usually see Class Klass just one package only, the real record is Klass specific meta-information in the template. This time to remember, young man.

3.2 workers

Xiaoqiang: Why are you the same amount of workers is poor height of it?
The same amount of workers

Old man: Your observation was quite careful Well! Yes, they are indeed the same amount of poor, want to know why, we must first understand what parts of these workers.

Workers constitution
They head size is fixed, body size is determined by their attribute data, and the last leg is for me to decide, if the size of the data of the previous two did not reach a multiple of eight, then I will be filled so here it is filled so that they have the same amount of height difference (memory alignment).

I have two reasons for this is to create them:

  1. Platform reason: Not all hardware platforms can access any data on any address; some hardware platforms can take only certain types of data at a certain address, hardware or throw an exception.
  2. For performance reasons: a central brain (CPU) to access memory is memory access granularity, each memory access is a fixed length, if you do not, then from the central brain requires two memory accesses, but only once after alignment.

Xiaoqiang: ah, I see! That can give me about these workers in residential areas Why should constantly move it?

3.3 growth

Old man: After a long period of observation, I found that the length of each worker's life is not the same. So I put into the new generation of residential areas, old time, and then let them reasonable relocation, this can make effective use of space junk and teams to work more efficiently.
Heap on behalf of distinction
After the birth of the workers will be assigned to the Eden area, Eden District staff when almost full, garbage squad will be cleaned, after cleaning, if the worker is still alive, then they will move to one area of Survivor, Survivor When this is almost full, garbage teams will be alive workers moved to another area in Survivor, so repeated, each time it is garbage cleaning squad, will be alive a year older workers, workers until the age of 15 years old, arrive after that they will move to old's place to live. But there are exceptions, if a worker eat too, the new generation can not tolerate him, then he will go directly to the old year stay. When old's lived fast, there will be garbage clean-up (full gc).

Xiaoqiang: I see ah! Since then, I no longer know rookie heap stack area of ​​it! Ha ha ha ha ......

Old man: Young man, do not be happy too early! You know so far is still a drop in the bucket.

3.4 Death Certificate

Xiaoqiang: How to determine whether the workers reach the end of life do?

The first: Reference counting

To add a reference counter for each worker, the worker is as long as people need help, then give the worker count is incremented by one, on the contrary, people are no longer needed to help the workers, the count is decremented by 1 until the count is 0, it indicates that the worker life to an end.

However, this method has a problem: if A and B workers, workers need to help each other, but no other worker or task requires the two of them, then the two of them will always live under! So we will not take this approach.

The second: reachability analysis

We identify the workers are called "GC roots" as a starting point, in order to find the workers they rely on the work, which may not need to know which workers are in existence any longer.

Xiaoqiang: how do I know which ones are "GC roots" workers?

The old man:

  1. Work area (stack) the need to use workers
  2. Warehouse (method area) in the template (class) itself, workers need the (static, constant)
  3. Workers (native method) behind the world needs

Xiaoqiang: Got it!

4. Recovery

Old man: I'll take you to know about garbage squad figure it! But before they know you better look at the basic methodology of garbage removal.

4.1 Basic methodology

The basic methodology followed garbage collection are the following:

  • Mark - Clear

    First all need to recycle the worker (objects), uniform recycling all workers marked after labeling is complete.

    But this has two drawbacks: 1. 2. inefficient will have a lot of space debris

  • copy

    The available space is divided into two, wherein each time only one when used up quickly, recovery teams, and then moved to the other workers alive one.

    Although this solves the mark - efficiency cleared, but this method has reduced by half the space.

  • Mark - finishing

    First all need to recycle the worker (objects), then the surviving workers to move to the end of the space, and then clean out the workers outside the boundary.

Xiaoqiang smiled: the original three algorithms ah! I know!

Old man: And if we know that with me to know about garbage cleaning team of people it!

4.2 key members

Scavenging team composed of several teams, customers like which team can specify who is going to work, cleaning their respective teams in different ways have advantages and disadvantages.

I introduce you to two key members of the bar, CMS, G1 two teams out of the line.

CMS: that we are CMS unit, full name "Concurrent Mark Sweep", as the name suggests, we are using labeled Clear squad concurrent algorithm, we pause to get the shortest recovery time objectives.

Xiaoqiang: Then tell me how you work?

CMS: Our work is divided into four steps, an initial mark concurrent mark 2. 3. 4. relabeled concurrent cleared.

Xiaoqiang: forget it, so many steps too take time to understand, and I now know your strengths, and shortcomings that what you have it?

CMS: This is also how people with exposing the scar ......

Then the old man a serious cough twice, which is intended to capture the CMS immediately, grievances, said:

I have three disadvantages:

  1. When a resource is not very adequate, take up too much resources, resulting in slow task
  2. When unable to deal with floating garbage, clean up our workers while also working, we mark, just some of the workers no longer needed
  3. Our units follow the "mark - sweep" algorithm, it will generate a lot of space debris, resulting in the world's clean-up (full gc) ahead of the arrival

Outspoken Xiaoqiang to the sentence: So your problems are so serious, the old man did not even put your squad quit ......

CMS: you ...... I remember when we smash hit squad but ......

So I guess G1 is not a CMS can make up for lack of it?

G1: To be honest, our goal is to replace the units CMS squad ...... (JDK14 CMS officially came to an end)

Xiaoqiang malicious laughed, ha ha ......, CMS roll their eyes and hide in the corner side of the secretly sad to go.

Corner crying
Xiaoqiang: G1 that talk about your ability now!

G1: Our team is sorting algorithm based on the mark, they will not produce large amounts of space debris

  • We also introduced the idea of ​​partition, weakening the concept of generational

  • We pause time is controlled, to avoid avalanche

  • We also give our customers can take full advantage of the resources and reduce standstill times

This is our team's strengths, then I'll give you a detailed briefing our team ......

Xiaoqiang: good! Go on……

return

Just when small strong interest in listening thick, the sky suddenly appeared a gigantic hand to hit him, and John Dodge is not open, ah ......

hand

Xiaoqiang holding his head, a little trance, looked up, rub, technical director ...... how you were here?

Director: I'm not here where I am? Sleep at home, you!

Then Xiaoqiang to collect myself, I realize I still in office, that something ah!

Director: Xiaoqiang, home to more cool, tomorrow will not be used to it!

Xiaoqiang a panic, his head had an idea: Director, I know just what to do it? That's not sleeping, I have a story and you hear make a decision.

It it it it ......

Finally, I will summarize the knowledge has become article Mind Mapping:
mind Mapping

Guess you like

Origin www.cnblogs.com/flyhero/p/12560412.html