Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity
preface
Recently because of the work task is more busy, their study plan also put aside a lot, through the activities of digging gold or to sort out some basic knowledge or read, understand some source knowledge to enrich themselves.
This series is part of understanding the Java Virtual Machine and writing process with reference to online materials and books.
The body of the
This article begins with a brief introduction to give you an overview of what a Java virtual machine does.
Java is a very familiar language. Java code can be run in many different ways. For example, it can be run in a development tool or run by double-clicking a JAR.
In fact, the JRE contains the necessary components for running Java programs, including the Java virtual machine, Java core class libraries, and so on.
What is the difference between Java running and C
Speaking of which, we all know that C code can be compiled directly into bytes read to the machine, whereas Java can’t. Typically, Java code is compiled into Java bytecode, which is read by the Java virtual machine and translated into machine-readable machine code.
For Android, it will be compiled into Dex files, and then executed by the Java virtual machine of the mobile system. So why Java should be run in the virtual machine and how efficient it is?
Why does Java run in a virtual machine
The Java Virtual Machine (JVM) generally provides software implementation on various platforms (such as Windows and Linux). It is an abstract software layer that shields the lower implementation from the upper Java code, so the idea is that once a program is compiled into Java bytecode, it can run on VMS of different platforms. This is the cross-platform nature of Java.
Another important point is that the Java virtual machine provides a hosting environment that can handle tedious and error-prone parts of code for us, such as memory management and garbage collection for the Java virtual machine, rather than languages like C++.
The Java virtual machine runs bytecode
Now that we know that the Java virtual machine is used to run Java bytecode, how does it work
Vm Perspective
To execute Java code, you need to load a. Class file into the Java virtual machine. Java is a class, so the loaded Java class is stored in the method area, and the virtual machine executes the code in the method area.
You’ll see something called a method area, where the Java virtual machine divides the heap and stack in memory to store runtime data, and the stack is subdivided into Java method stacks for Java methods, local method stacks for local methods, and PC registers for each thread’s execution location.
The general structure is as follows:
There’s a good reason why we do this, and we’ll talk more about it later when we talk about the memory model.
Whenever a Java method is called during a virtual machine run, the Java virtual machine generates a stack frame in the current thread’s Java method stack that holds local variables and a tree of bytecode operations, and pops up when exiting the currently executing method.
Hardware point of view
Java bytecode cannot be executed directly and needs to be translated by the Java VIRTUAL machine. In HotSpot, translation is divided into two types: interpreted execution, which translates bytecode into machine code one by one and executes it, and just-in-time compilation, which compiles all bytecode contained in a method into machine code and executes it.
The advantage is that explain execution does not have to wait for compilation, whereas just-in-time compilation is actually faster at run time because it is translated. HotSpot defaults to hybrid mode.
Operation efficiency
The two translation methods mentioned above should be understood thoroughly, which directly affects the running efficiency of the Java virtual machine. We can understand so, explain to perform less memory, memory and instantaneous compiling, but more quickly, so the Java virtual machine adopts reasonable mechanisms, to make for occupying most of the code we don’t need to waste time not commonly used to compile it into machine code, but using interpretive execution, and for a small number of hot code, it can be translated into machine code, And from speed up the operation.
In the Java background, code runs for a long time, so if the Java VIRTUAL machine uses the just-in-time compiler, the Java virtual machine will keep information about the program running, and dynamically learn which code is hot and needs to be compiled just-in-time to speed up the process without taking up more memory, thus achieving a balance.
For Android applications, because apps need to be started frequently and not run for a long time, the mode of AOT(Ahead of Time compilation) was adopted in Android 5.0. This mode means that Java bytecode is compiled into machine code offline, which is faster when running. When do you compile it? Right when you install the APP, so it takes a little bit longer to install, but it’s faster when you start the APP.
conclusion
This article is just a primer to understand why Java virtual machines are needed, that Java virtual machines have several parts in memory, and that good Java virtual machines combine different translation methods to reach peak performance.