Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basics of JVM?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you what is the basic knowledge about JVM. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Virtual machines feel like operating systems and compilers: very high-end. But the Java program is running on it, and you still have to troubleshoot when you encounter problems, and you have to optimize it if the performance is not good. Basic knowledge is still needed!

Memory management

During execution, the Java virtual machine divides the memory it manages into several different data regions, roughly as follows:

The functions of each part are as follows:

The larger part of the memory management section is GC (garbage collection), which is to recycle the memory occupied by garbage. So the first question: what is garbage?

Reference counting algorithm: an object whose number of references is 0.

Root search algorithm: objects that cannot be found along references from GC Roots.

References are all mentioned here, and Java has extended the concept of references since JDK 1.2, so the second question: what types of references are there?

Strong references: Object o = new Object () are all strong references.

Weak references: also useful but not necessary, recycled before OOM.

Soft references: weaker references that are recycled at the next GC.

False reference: the weakest, the only function is to be notified when the object is reclaimed.

Only strong references can affect the lifecycle of an object. In the process of virtual machine development, many garbage collection algorithms have been evolved, such as:

Mark-clear algorithm

Replication algorithm

Marking-finishing algorithm

Generation collection algorithm

The recyclers used in practice are all a combination of these algorithms, for example, the memory seen in VisualVM looks like this (you need to understand how the parts work together):

On the whole, it is a generational collection algorithm, while S0 and S1 can be regarded as mark-finishing algorithms. So the third question: what is the execution process of the common CMS garbage collector?

Initial tag: an object directly associated with GC Roots.

Concurrent tag: Root Tracing.

Relabeling: fixed markup changes due to program running.

Concurrent cleanup

It is shown in the following figure:

You can see that Stop The World is only needed for initial markup and relabeling, and the rest is executed with the user thread, so don't think it's perfect, the process of executing in parallel will consume some CPU resources.

Code execution

Throwing the Java source code to JVM must not be executed, and you need to compile it into a class file with javac first, so the first question: what is the structure of the class file?

Constant pool

Access flag

Class index, parent index, and interface index

Field table

Method table

Property sheet

The virtual machine specification does not specify when classes should be loaded, but it does specify that initialization is required when new, reflection, parent class, and Main are encountered. The life cycle of the entire class is as follows:

To load the class through ClassLoader in the virtual machine, you need to understand:

Whether the two classes are the same or not, you need to determine whether the ClassLoader is the same in addition to the class name.

The parental delegation model is not a mandatory constraint.

Execution can begin after the class is loaded, and everything related to thread operation is placed in the stack frame, which is structured as follows:

Which method is called during execution is a headache and needs to be dealt with:

Static dispatch: a method with the same name and different parameter types.

Dynamic dispatch: a method of overwriting in inheritance.

The instructions in the bytecode are all stack-based operations, for example, to complete calculations such as 1: 1, the corresponding instructions are as follows:

Iconst_1 / / push the constant 1 into the stack iconst_1iadd / / add the two values at the top of the stack and out of the stack, and then put the result back into the stack istore_0 / / put the value at the top of the stack to the 0th Solt of the local variable scale

The advantage of explanation execution is that it starts quickly after download, but the determination is also very obvious: it runs slowly. JIT is used to solve this problem by compiling methods that are called many times and loop bodies that are executed many times into native code.

Optimization is a very interesting topic. Remember to take part in a competition and use the compiled code of gcc-O3 to output printf (). The common optimization methods in JIT are:

The execution of the program must involve memory operations, and eight operations are defined in Java:

It is necessary to talk about the role of volatile. When you use it, you can understand the following two items:

Make sure that the variable is visible to all threads.

Rearranging instruction optimization is prohibited.

If all the operations in Java require programmers to control, there will be a lot of repetitive code and it is tiring to write, so we can judge whether there is a conflict between the two parallel operations by the principle of prior occurrence:

Program order rules: in a single thread according to the program writing order.

Pipe locking rule: unlock must precede lock.

Volatile variable rule: the write operation occurs first in the read operation.

Thread startup rule: any other method that Thread.start () precedes a thread.

Thread termination rule: all operations in a thread precede the termination detection of this thread.

Thread interrupt rule: interrupt () precedes interrupt detection.

Object termination rule: the initialization of an object precedes its finalize () method.

Transmission rule: if A precedes B and B precedes C, then A precedes C.

The underlying implementation of Thread is cumbersome, but at the very least you should know how the state of Thread is transformed:

Finally, the common synchronization methods are various implementations of synchronized or aqs.

The above is the basic knowledge about JVM shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report