Architect's Perspective: Analyzing the JVM Architecture

Every Java developer knows that bytecode is executed by JRE (Java Runtime Environment). But many people don't know that JRE is an implementation of the Java Virtual Machine (JVM), which is responsible for analyzing bytecode, parsing, and executing the code. Understanding the JVM architecture is very important as a developer because it enables us to write code more efficiently. In this article we will take a more in-depth look at the JVM architecture in Java and the various components of the JVM.

What is JVM?

A virtual machine is a software implementation of a physical machine. Java runs on a VM and implements WORA (Write One, Run Anywhere). The compiler compiles the Java file into a Java .class file, and then the .class file is input into the JVM for loading and execution of the class file. The following is an architecture diagram of a JVM.

How does the JVM work?

As shown in the architecture diagram above, the JVM is divided into three main subsystems:

  1. class loader subsystem

  2. runtime data area

  3. execution engine

1. Class loader subsystem

Java's dynamic class loading functionality is handled by the class loader subsystem. When it first references a class at runtime (not compile time), it loads, links, and initializes the class file.

1.1 Loading

The class is loaded by this component. Start class loader (Boot Strap class Loader), extension class loader (Extension class Loader) and application class loader (Application class Loader) These three class loaders help complete the loading of classes.

  1. Startup classloader – responsible for loading classes from the startup classpath, nothing more than rt.jar. This loader will be given the highest priority.

  2. Extension class loader – responsible for loading classes in the ext directory (jre\lib).

  3. Application class loader – Responsible for loading application level class paths, environment variables related to paths, etc.

The above class loader will follow the Delegation Hierarchy Algorithm to load class files.

1.2 Links

  1. Verification – The bytecode verifier will verify that the generated bytecode is correct, if the verification fails, we will get a verification error.

  2. Prepare - allocate memory and initialize default values ​​for all static variables.

  3. Resolution - All symbolic memory references are replaced by the original references to the Method Area.

1.3 Initialization

This is the final stage of class loading, where all static variables will be initialized and static blocks will be executed.

2. Runtime Data Area

The runtime data area is divided into 5 main components:

  1. Method Area  - All class level data will be stored here, including static variables. There is only one method area per JVM, which is a shared resource.

  2. Heap Area  - All objects and their corresponding instance variables and arrays will be stored here. Each JVM also has only one heap area. Since the memory of the method area and heap area is shared by multiple threads, the stored data is not thread-safe.

  3. Stack Area  - A separate runtime stack is created for each thread. A stack frame is generated in stack memory for each function call. All local variables will be created in stack memory. The stack area is thread-safe because it is not a shared resource. The stack frame is divided into three sub-entities:

    1. Local variable array  – how many local variables related to the method and corresponding values ​​will be stored here.

    2. Operand stack  – If any intermediate operations need to be performed, the operand stack acts as a runtime workspace to execute instructions.

    3. Frame data  – all symbols for the method are kept here. In the case of any exception, the catch block information will be stored in the frame data.

    4. PC register  – each thread has a separate PC register to hold the address of the currently executing instruction, once the instruction is executed, the pc register will be updated to the address of the next instruction.

    5.  Native Method Stack  – The native method stack holds native method information. For each thread, a separate native method stack will be created.

3. Execution engine

The bytecode allocated to the runtime data area will be executed by the execution engine. The execution engine reads the bytecode and executes it segment by segment.

  1. Interpreter  – Interpreters can interpret bytecode quickly, but execute very slowly. The disadvantage of the interpreter is that when a method is called multiple times, it needs to be reinterpreted each time.

  2. JIT Compiler  – A JIT compiler eliminates the shortcomings of interpreters. The execution engine uses the interpreter to convert the bytecode, but if the code is repeated, the JIT compiler is used to compile the entire bytecode into native code. Native code will be used directly for repeated method calls, which improves the performance of the system.

1. Intermediate code generator  – generate intermediate code

2. Code optimizer  – responsible for optimizing the intermediate code generated above

3. Object code generator  – responsible for generating machine code or native code

4. Profiler  - A special component that looks for methods that are called multiple times.

3. Garbage collector : Collect and delete unreferenced objects. Garbage collection can be triggered by calling "System.gc()", but there is no guarantee that garbage collection will actually occur. The JVM's garbage collection only collects objects created by the new keyword. So, if the object was not created with new, you can use the finalize function to perform cleanup.

How to become a pillar-like architect of a company?

basic knowledge

1. Learn to analyze source code

Programmers work with code every day. After years of basic education and vocational training, most programmers "write" code, or at least copy and modify code. However, there are not many people who can read code. There are very few people who can read code and really understand the source code of some large projects. This kind of weirdness really has to be investigated, no wonder the group of programmers itself - it is caused by two reasons:

  • All of our education and training emphasizes how to code, not how to read code

  • Most work scenarios are one radish and one pit. We only need to understand a part of a system to work. Reading irrelevant code seems useless.

Read the source code and ask three questions: "Why does it have such an architecture", "What does it look like", "How does it work".

So how do Ali programmers read code?

2. Distributed architecture features and design concepts

The first thing to note is that distributed systems is a complex and broad field of study, and taking one or two online courses or reading a book or two may not cover it all. Since this article is a guide for beginners to get started, I personally think that it may be more helpful to introduce the whole picture of the current distributed system field for beginners than to directly recommend papers and courses. When beginners have established a large picture in this field, they can selectively go deep into different fields for further study according to their own interests.

3. Why are microservices so popular?

To learn about microservices, first, we need to understand why microservices are used.

  • Code difficult to understand?

  • Construction and deployment take a long time, difficult to locate problems, and development efficiency is low?

  • A single unit can only expand horizontally as a whole, but cannot expand vertically by modules?

  • Could a bug cause the entire app to crash?

  • Limited by the technology stack, team members use the same framework and language?

So how to solve the insufficiency of the monolith? By migrating to the microservice architecture, let's take a look at what a microservice is.

Microservice architecture: split a single application into multiple small services with high cohesion and low coupling, each small service runs in an independent process, developed and maintained by different teams, and uses a lightweight communication mechanism between services, which is independent and automatic Deployment can be in different languages ​​and storage.

Monolithic architecture The entire team maintains and develops a large project and a single library. In the microservice architecture, user requests are routed to downstream services through API Gateway, and services communicate with each other through a lightweight communication protocol. Services discover each other through the registry. Each service has a dedicated development and maintenance team, and each service corresponds to an independent database. The service is independently developed, deployed and launched independently.

Next, we summarize the advantages of microservices.

  • Easy to develop and maintain

  • Microservices are relatively small and easy to understand

  • Short startup time and high development efficiency

  • Standalone deployment

  • Modification of one microservice does not require coordination with other services

  • Strong scalability

  • Each service can be scaled horizontally and vertically

  • Each service can be independently scaled according to hardware resource requirements

  • match the organizational structure

  • Microservices architecture can better match architecture and organization

  • Each team is responsible for certain services independently, resulting in higher productivity

  • technological heterogeneity

  • Use the technology best suited for the service

  • Reduce the cost of trying new technologies

Let's send the learning structure diagram below.

If you feel that you want to improve yourself and learn the knowledge in the article, here is a place to recommend a free open class, you can join the group: 433540541, find the group owner to obtain the class qualification, this is a free course, you can be polite when looking for the group leader a little.

4. Should programmers learn JVM or not?

There are always people asking if this thing doesn't seem to work, so it's a question of whether to learn it.

Then there are always people who worry about doing things that are repeated and not improved all day long.

If you are only willing to be a mediocre Java coder in your life, then there is no need for you to learn JVM-related knowledge. The benefits of learning JVM for a Java programmer can be summarized as follows:

  • 1. You can understand why Java was originally called an interpreted language, and then why it was called a language that coexists with interpretation and compilation (understanding the interpreter and just-in-time compiler in the JVM can answer this question);

  • 2. You can understand the difference between dynamic compilation and static compilation, and what are the benefits of dynamic compilation over static compilation (JVM JIT);

  • 3. You can use some tools, such as jmap, jvisualvm, jstat, jconsole and other tools to help you observe the layout of the Java application heap at runtime, so you can improve the performance of Java applications by adjusting JVM-related parameters;

  • 4. You can clearly know how the Java program is executed;

  • 5. You can understand why high-level languages ​​such as Java have the characteristics of strong portability.

In fact, this question is equivalent to "Why do C/C++ programmers need to learn architecture and compilation principles?"

Not much to say, attach the learning system diagram

5. Engineering topics that we ignore

The segmentation of the IT industry is not a matter of one or two days. The integration of technology is not a shameful thing, but another valuable capability. It's not like some people describe it as if you wholesale a few CPUs and get Huaqiangbei to convert your computer into a supercomputer.

So why do we often overlook the value of engineering? The main reason, perhaps, is that engineering itself is too far away from us. The higher the universality of an industry's engineering, the more mature the development of the industry is: the subdivision of the industrial chain, the refinement of the division of labor, and the emergence of efficient working methods such as globalized R&D and production. The maturity of the industry also often represents a significant oligopoly.

In the IT industry, oligarchy means fewer startups—no one has to tell stories with big press conferences, no one has to advertise how much money they have raised.

The education of this generation of Chinese since childhood is not comparable to that of STEAM in Europe and the United States, but emphasizes academics and light craftsmanship. We tend to equate engineering with excess capacity. Strong capital and technical thresholds cast a mysterious veil on these industries, making it difficult for ordinary people to truly understand the complexity of the technology and craftsmanship, and even more difficult to understand the value. But it is precisely because of China's engineering capabilities that we have the opportunity to reach the first echelon of the AI ​​era, not just academic research capabilities.

Another reason may be that we are born with a "rebellious mind". Industries with high technical thresholds, such as supercomputers and mobile phone chips, are often backed by large enterprises and state-owned scientific research institutions. When the object of judgment is them, we seem to be more willing to believe in dog-blooded business stories and conspiracy theories: for example, scientific research funds are eaten and drunk by professors; supercomputers are satellites, but the United States and Japan don’t care at all; XX companies’ technology They are all bought from startups and have no technology other than making money from users...

The reason for this kind of "rebelliousness" is too profound. All we can do is to hold down our hand running towards the keyboard when this "habitual thinking" appears, turn expressing desire into curiosity, and fulfill our obligation to understand. , and then exercise their right to criticize.

Attach a mind map

6. Without high concurrency experience, what should I do if I want to join a big company?

What if you don’t have a reliable company and can’t get in touch with high-concurrency business scenarios? You always solve small problems, and your technology may not improve much after 10 years of work.

Many programmers often come to me and say that without experience, there is no reliable company, and without a reliable company, there is no experience. I have read countless books and done countless experiments myself, desperately trying to find a reliable company. In-depth, but it feels so difficult, it's an endless loop

Friends of the readership group are more concerned about high concurrency. The reason is very simple. If you want to go to a large company like BAT, you must have high concurrency experience. Today, the knowledge of high concurrency is popularized, and I hope everyone has a correct understanding of high concurrency.

7. Learning a thousand times is not as good as a successful project

One of the most common mistakes we make in the process of learning is: watching more and doing less. Especially for the overall development of some projects, we have fewer opportunities to contact.

A complete development is the best learning. It allows you to have a complete understanding of the entire development process, and the knowledge will be greatly consolidated. More importantly, you will learn how to apply theoretical knowledge to practical development.

So no matter the size of the project, you must start to develop and learn.

I believe that many programmers will have some in the actual project, but what do we need to learn?

It depends on whether you want to be an architect or not, why 98% of programmers work for 10 years and are just a developer all their lives. Programmers should think about this question, do I need to improve.

In my opinion, the most important thing to learn project practice is to learn project management. As a programmer, you should learn some project management.

  1. Everything is a "project"

  2. Two types of properties of the project (complex logic, huge amount of information)

  3. The human brain is good at thinking, not memory

  4. Become a "one-sided" person

Being alone is a very sexy word. Whether you have it or not, the corresponding workplace value, has a world of difference.

All bosses like employees who are "in charge of themselves", because this is the most labor-saving and the best way to settle accounts: give you a resource, give you a title, give you a goal, and then you give me a piece of the world.

When you can be independently responsible for a bunch of things and get them done one by one, you will have a large workplace premium-correspondingly, the return on income is far from being comparable to "technical screws".

If you are aggressive, you will gradually: lead a group, a department, a family, or even a city... And the starting point of all this is to do a project independently and completely: you have no one to rely on, you need to Take responsibility for things big and small, and you're responsible for the end result.

In other words, "project management" is a meta-capacity that "stands alone". During this process, your consciousness will become clearer, your methodology will become more mature, your confidence will become more abundant, and the project will become bigger and bigger. Until one day, you really have a frontier official who controls one side.

This is the ultimate meaning of our learning "project combat".

Maybe as a programmer, you want to improve yourself, but you can't find a breakthrough, and no one takes you in the company. Or maybe you have been working for 6 years, but you are still very confused, you still do not understand a lot of knowledge, and you have not met your desired position and salary. Here is a place to recommend a free open class. The basic knowledge points of architects mentioned above have information. You can join the group: 433540541. Find the group owner to obtain the class qualification. This is a free course. You can be polite when looking for the group leader. a little.

At this point, you may think that the article is over. After learning these, you can go to a large BAT company to be an architect with an annual salary of 50W+?

No, you are wrong, these are the most basic knowledge. To become an architect must be a cumulative process, and so many programmers are only a developer in their entire life, and they will be fired by the company when they get old.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325155313&siteId=291194637