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

How does JVM work?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how JVM works. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

In JDK1.7 and before, we all use Sun's HotSpot, but as both Sun and BEA are acquired by oracle, jdk1.8 will use the essence of Sun's HotSpot and BEA's JRockit to form jdk1.8 's JVM.

We all know that the slogan that java has been promoting is: compile once, run everywhere. So how does it work? Let's take a look at the following picture:

Let's look at a piece of code first:

Public class HelloWorld {

Public static void main (String [] args) {

System.out.print ("Hello world")

}

}

What steps did this program go through from compiling to running and finally printing out "Hello world"? Let's go straight to the picture above:

Let's not discuss the problem of class loading here.

Life cycle of JVM

1. Activate. Start a Java program and an instance of JVM is generated. Class with the public static void main (String [] args) function can be used as a starting point for JVM instances to run.

two。 Run. Main () is the starting point of the program's initial thread, and any other thread can be started by that thread. There are two kinds of threads inside JVM: daemon thread and non-daemon thread, main () belongs to non-daemon thread, daemon thread is usually used by JVM, and the program can specify that the created thread is daemon thread.

3. Die. JVM exits when all non-daemon threads in the program terminate; the program can also exit using the Runtime class or System.exit () if the security manager allows it.

The example of the JVM execution engine corresponds to the thread that belongs to the user's running program, which is thread-level.

The architecture of JVM

Data runtime area of JVM

The main purpose of JVM tuning is to optimize Heap heap and Method Area method area.

Method Area method area

The method area is shared by all threads, all fields and method bytecodes, and some special methods such as constructors, and the interface code is also defined here. To put it simply, the information about all defined methods is stored in this area, which belongs to the shared area.

Static variable + constant + class information + runtime constant pool exists in the method area, and instance variables are stored in heap memory.

Stack stack

The data in the stack exists in the format of Stack Frame, which is a memory block, a data set, a data set about methods and runtime data. When a method An is called, a stack frame F1 is generated and pushed into the stack, and method A calls method B, so the stack frame F2 is also pushed into the stack, and method B calls the C method, so the stack frame F3 is also pressed into the stack. After the execution is finished in turn, first eject and then enter the .F3 stack frame, then eject the F2 stack frame, and then pop the F1 stack frame.

Follow the "first in, last out" / "last in, first out" principle.

Heap reactor

Heap is the largest area in JVM, and the applied objects and data all exist in this area. This area is also shared by threads, and it is also the main recycling area of gc. There is only one heap memory in a JVM instance, and the size of heap memory can be adjusted. After the class loader reads the class file, it needs to put the classes, methods, and constants in the heap memory to facilitate execution by the executor. Heap memory is divided into three parts:

① newborn area

The newborn area is the area where classes are born, grow and die, where a class is generated, applied, and finally collected by the garbage collector to end life. The newborn zone is divided into two parts: Eden space and Survivor pace. All the classes are new in Eden. There are two survival areas: zone 0 (Survivor 0 space) and zone 1 (Survivor 1 space). When the space in the Garden of Eden runs out, the program needs to create objects again, and JVM's garbage collector will Minor GC the Garden of Eden, moving the remaining objects in the Garden of Eden to Survivor 0. If the surviving area 0 is also full, garbage collection will be carried out in the area, and then moved to area 1. What if 1 is full? And then move to the nursing area. If the nursing area is also full, then Major GC (FullGCC) will be generated at this time to clean up the memory of the nursing area. If it is found that the object cannot be saved after performing Full GC in the nursing area, an OOM exception "OutOfMemoryError" will be generated.

If a java.lang.OutOfMemoryError: Java heap space exception occurs, the Java virtual machine does not have enough heap memory. There are two reasons:

The heap memory setting of the a.Java virtual machine is not enough and can be adjusted by the parameters-Xms and-Xmx.

b. A large number of large objects are created in the code and cannot be collected by the garbage collector for a long time (there are references).

② retirement area

The nursing area is used to store JAVA objects filtered from the newborn area, and pool objects are generally active in this area.

③ permanent zone

The permanent storage area is a resident memory area, which is used to store the Class,Interface metadata carried by JDK itself, that is, it stores the class information necessary for the running environment. The data loaded into this area will not be reclaimed by the garbage collector. Closing JVM will release the memory occupied by this area.

If java.lang.OutOfMemoryError: PermGen space appears, the Java virtual machine does not have enough memory settings for the permanent Perm. There are two reasons:

a. A large number of third-party jar packages need to be loaded to start the program. For example, too many applications are deployed under one Tomcat.

b. A large number of classes generated by dynamic reflection are constantly loaded, resulting in the Perm area being occupied.

JVM garbage collection

(1) reference count (Reference Counting):

A relatively old recycling algorithm. The principle is that this object has a reference, which increases a count, and deletes a reference reduces a count. For garbage collection, only objects with a count of 0 are collected. The deadliest thing about this algorithm is that it can't handle circular references.

(II) marking-clearing (Mark-Sweep):

The implementation of this algorithm is divided into two stages. The first phase marks all referenced objects from the reference root node, and the second phase traverses the entire heap to clear the untagged objects. This algorithm needs to pause the entire application and generate memory fragmentation at the same time.

(3) copy (Copying):

This algorithm divides the memory space into two equal regions, using only one of them at a time. When garbage collection, traverse the current use area and copy the objects in use to another area. The algorithm only deals with the objects in use each time, so the replication cost is relatively small, and the corresponding memory can be demarcated after the copy, without the problem of "fragmentation". Of course, the disadvantage of this algorithm is also obvious, which requires twice the memory space.

(IV) marking-finishing (Mark-Compact):

This algorithm combines the advantages of "mark-clear" and "copy" algorithms. It is also divided into two stages, the first stage marks all the referenced objects from the root node, and the second stage traverses the entire heap, clearing the untagged objects and "compressing" the living objects into one of the pieces of the heap and discharging them sequentially. This algorithm not only avoids the fragmentation problem of "mark-clear", but also avoids the space problem of "copy" algorithm.

This is the end of the article on "how JVM works". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Internet Technology

Wechat

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

12
Report