Writing in the front
Every Java developer knows that bytecode is executed by the JRE (Java runtime environment). But many people don’t know that the JRE is an implementation of the Java Virtual Machine (JVM), which is responsible for parsing bytecode, parsing, and executing code. Understanding the JVM architecture is important as a developer because it allows us to write code more efficiently. In this article we’ll take a closer look at the JVM architecture in Java and the various components of the JVM.
What is the JVM?
A virtual machine is a software implementation of a physical machine. Java runs on a VM and implements WORA (written in one place, run everywhere). The compiler compiles the Java file into a java.class file, which is then entered into the JVM for class file loading and execution. The following is an architecture diagram of a JVM.
How does the JVM work?
As the architecture diagram above shows, the JVM is divided into three main subsystems:
Class loader subsystem
Runtime data area
Execution engine
1. Classloader subsystem
Java’s dynamic classloading capability is handled by the classloader subsystem. When it first references a class at run time (not compile time), it loads, links, and initializes the class file.
1.1 loading
Classes are loaded from this component. The Boot Strap Class Loader, Extension Class Loader, and Application Class Loader help complete the loading of classes.
Launch classloader – responsible for loading classes from the launch classpath, nothing more than rt.jar. The loader is given the highest priority.
Extension class loader – responsible for loading classes in the Ext directory (jre\lib).
Application class loader – responsible for loading the application level classpath, including environment variables related to the path, etc.
The class loader follows the Delegation Hierarchy Algorithm to load the class file.
1.2 the link
Validation – The bytecode validator verifies that the generated bytecode is correct, and if the validation fails, we get a validation error.
Preparation – Allocates memory and initializes default values for all static variables.
Parsing – All symbolic memory references are replaced by original references to the Method Area.
1.3 the initialization
This is the final stage of class loading, where all static variables are assigned initial values and static blocks are executed.
2. Runtime Data Area
The runtime data area is divided into five main components:
Method Area – This is where all class-level data will be stored, including static variables. Each JVM has only one method area, which is a shared resource.
Heap Area – This is where all objects and their corresponding instance variables and arrays will be stored. Each JVM also has only one heap area. Because the memory in the method area and heap area is shared by multiple threads, the stored data is not thread-safe.
Stack Area – A separate runtime Stack is created for each thread. Calling each function generates a Stack Frame in Stack memory. All local variables are created in stack memory. The stack area is thread-safe because it is not a shared resource. Stack frames are divided into three child entities:
1. Local variable array – Contains how many local variables related to the method and the 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 saved 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 that holds the address of the current executing instruction. Once the instruction is executed, the PC register is updated to the address of the next instruction.
5. Local method stack – The local method stack holds local method information. For each thread, a separate local method stack is created.
3. Execution engine
The bytecode assigned to the runtime data area is executed by the execution engine. The execution engine reads the bytecode and executes it segment by segment.
Interpreters – Interpreters interpret bytecode quickly, but execute slowly. The disadvantage of the interpreter is that when a method is called multiple times, it needs to be reinterpreted each time.
JIT compilers – JIT compilers eliminate the interpreter’s disadvantages. The execution engine converts bytecode using the interpreter, but if the code is repetitive it compiles the entire bytecode into machine code using a JIT compiler. Native code is used directly for repeated method calls, which improves system performance.
1. Intermediate Code Generator – Generates intermediate code
2. Code optimizer – is responsible for optimizing the intermediate code generated above
3. Object Code generator – Responsible for generating machine or native code
4. Profiler – a special component that looks for methods that are called multiple times.
5. Garbage collector: Collects and deletes unreferenced objects. Garbage collection can be triggered by calling “system.gc ()”, but there is no guarantee that it will actually take place. The JVM’s garbage collection only collects objects created by the new keyword. So, if objects are not created with new, you can use finalize function to clean up.
How do you become the backbone of a company as an architect?
Basic knowledge of
1. Learn to analyze source code
Programmers work with code every day. After years of basic education and professional training, most programmers can “write” code, or at least copy and modify code. However, can read the code is not in the majority, can read the code and really understand the source code of some big projects, very few. This strange situation, if it is to be investigated, can be blamed on the programmer community itself — it is caused by two reasons:
All of our education and training is about how to write code, not how to read code, right
Most work scenarios are a patchwork, where we only need to understand parts of a system to work, and reading irrelevant code doesn’t seem to work
Read the source code three ask: “why is there such a architecture”, “what does it look like”, “how does it work”.
2. Distributed architecture features and design concept
First of all, distributed systems are a complex and broad field of study that may not be covered by a course or two online or a book or two. Since this article is for beginners to get started, I personally feel that it may be more helpful to give beginners an overview of the current field of distributed systems than to directly recommend papers and courses. Once a scholar has established a big Picture in this field, he or she can selectively go into different fields for further study according to his or her interests.
3. Why are microservices so popular?
To learn about microservices, first we need to understand why we use microservices.
Code hard to understand?
Is it time-consuming to build and deploy, difficult to locate problems, and inefficient to develop?
A single unit can only be extended horizontally as a whole, not vertically by modules.
Can a bug cause an entire application to crash?
Constrained by the technology stack, team members using the same framework and language?
So how do you solve the singleton problem by moving to a microservice architecture, and let’s look at what microservices are.
Microservice architecture: Single applications are divided into multiple small services with high cohesion and low coupling. Each small service runs in an independent process and is developed and maintained by different teams. Lightweight communication mechanism is adopted between services, independent automatic deployment can be adopted, and different languages and storage can be adopted.
Monomer architecture to the whole team to develop a large maintenance project and a single library, the service architecture, user requests through the API Gateway is routed to the downstream service, with lightweight communication protocol for communication between services, service registry found each other, each service has the development of specialized maintenance team, each corresponding to a separate database service, Services are independently developed, deployed and launched.
Next, we summarize the advantages of microservices.
Easy to develop and maintain
Microservices are relatively small and easy to understand
Short start-up time, high development efficiency
Independent deployment
Changes to one microservice do not require coordination with other services
Strong scalability
Each service can scale horizontally and vertically
Each service can be independently expanded based on hardware resource requirements
Matches the organizational structure
Microservices architectures can better match architecture and organization
Each team is individually responsible for certain services, resulting in higher productivity
Technical heterogeneity
Use the technology best suited to the service
Reduce the cost of trying new technologies
4. Should programmers learn the JVM at all
There are always people asking this thing doesn’t seem to be useful, so should we learn such questions?
And then there’s always the worry of moving bricks all the time and doing the same thing all the time.
If you want to be a mediocre Java programmer for the rest of your life, there is no need to learn about the JVM. The benefits of learning the JVM for a Java programmer can be summarized as follows:
1. You can see why Java was originally called an interpreted language and why it has since been called an interpreted and compiled language (understanding interpreters and just-in-time compilers in the JVM can answer this question);
2. You understand the difference between dynamic compilation and static compilation, and the benefits of dynamic compilation over static compilation (JVM JIT);
3. You can use jMap, JVisualVM, Jstat, JConsole, etc. to help you observe the heap layout of your Java application at runtime, so that you can adjust JVM parameters to improve the performance of your Java application.
4. You can clearly understand how Java programs are executed.
5. You can understand why Java and other high-level languages have strong portability.
This is the equivalent of “Why do C/C++ programmers need to learn architecture and compilation principles?”
5. Engineering projects we neglected
The fragmentation of the IT industry has been a long time. Integration technology is not a shameful thing, but another valuable ability. It is not as some people described, as if the wholesale of a few CPUS, take Huaqiangbei can modify their computer into a supercomputer.
So why do we so often ignore the value of engineering? The main reason, perhaps, is that engineering itself is so far away. The higher the universality of an industry engineering, the more mature the industry development: industry chain subdivision, division of labor, global RESEARCH and development and production such as efficient work methods began to appear. Industry maturity also often represents a significant oligopolization.
In the IT industry, oligopolization is a sign of fewer startups — no one to tell stories with big press releases and advertise how much funding they’ve raised.
The education of this generation of Chinese is not like that of STEAM in Europe and The United States. We tend to equate engineering with overcapacity. Strong capital and technological barriers create a veil of secrecy around these industries, making it difficult for ordinary people to truly appreciate the complexity of the technologies and processes involved, and even harder to understand their value. But it is because of China’s engineering capabilities that we have the opportunity to move to the first tier of the AI era, not just academic research capabilities.
Another reason may lie in our innate rebelliousness. Large enterprises and state-funded research institutions are often behind industries with high technological thresholds such as supercomputers and mobile phone chips. When judged against them, we seem more likely to believe dodgy business stories and conspiracy theories: that research funding is being eaten and drunk by professors; Make supercomputer is put satellite in fact the United States and Japan do not care at all; The technology of XX enterprise is all bought from start-up companies. There is no technology except to make money from users.
The reason for this “rebellious heart” is so profound that we can only hold down our hands running to the keyboard when this “habitual thinking” appears, turn desire into curiosity, fulfill the obligation of understanding, and then exercise our right to criticize.
6. Without high concurrency experience, what should I do if I want to join a big company?
What if there is no reliable company and no access to high concurrency business scenarios? You always solve small problems, and 10 years of work doesn’t necessarily improve your skills.
Many programmers often come to me and say that there is no reliable company without experience, and there is no reliable company without experience. I read countless books and did countless experiments to try my best to find a reliable company to go deep, but I feel it is so difficult, it is a cycle
Readers of friends are more concerned about high concurrency, the reason is simple, want to go to BAT such a large company, you must have high concurrency experience. Today popularized the knowledge of high concurrency, I hope you have a correct understanding of high concurrency.
Study a thousand times, not as successful as the project actual combat once
One of the most common mistakes we make in the learning process is to see too much and do too little. Especially for the overall development of some projects, we have even less exposure.
A complete development is the best learning. It gives you a complete view of the entire development process and a great consolidation of knowledge. More importantly, you will learn how to apply theoretical knowledge to practical development.
So no matter how big or small a project is, be sure to get your hands dirty and learn.
Project combat I believe many programmers are how much will have, but we have to learn what?
It depends on whether you want to be an architect or not, why 98% of programmers work for 10 years and remain a developer all their lives. Programmers have to think about whether I need to improve.
In my opinion, the most important thing to learn project actual combat is to learn project management. As a programmer, we should learn project management.
Everything is a project
Two types of project attributes (complex logic, large amount of information)
The human brain is good at thinking, not remembering
Be a “man of my own.
Being alone is a very sexy word. The workplace value of having it or not is vastly different.
All bosses love “do-it-yourself” employees because it is the most effortless and best accounting model: give you a resource, give you a title, give you a goal, and you give me a piece of the world.
When you can take sole responsibility for a whole bunch of things and get them done, there’s a huge workplace premium — and, in turn, a far bigger payback than the technical screws.
If you are aggressive, you will dominate a group, a department, a family, a city… The starting point for all of this is to do a project as a whole: you don’t have anyone to rely on, you’re responsible for everything big and small, and you’re responsible for the end result.
In other words, “project management” is a “do-it-yourself” meta-capability. In the process, your awareness grows clearer, your methodology grows more mature, your confidence grows, and your projects grow bigger. Until one day, you do have a fief who controls one side.
This is the ultimate purpose of studying project practice.
Maybe you want to improve as a programmer, but you can’t find a breakthrough. Or maybe you’ve been working for 6 years, but you still don’t know much about it, and you haven’t met your expectations for a position or salary.
The end?
By now, you may think that the article is over. After learning these, you can go to BAT company to be an architect with an annual salary of 50W+?
No, you’re wrong. These are the basics. Becoming an architect must be a cumulative process.
All the information mentioned above, JVM, high concurrency and so on and related interview questions, xiaobian has been sorted out, interested friends can check the blogger’s home page to get free