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 is the architecture and memory model of the Java virtual machine

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the architecture and memory model of the Java virtual machine". In the daily operation, I believe that many people have doubts about the architecture and memory model of the Java virtual machine. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the architecture and memory model of the Java virtual machine?" Next, please follow the editor to study!

Java virtual machine architecture

Class loading subsystem

There are two types of loaders for Java virtual machines, namely boot class loader and user-defined loader.

The general class loading subsystem loads Class into the runtime data area through the fully qualified name of the class (package name and class name, and network loading also includes URL). For each loaded type, the Java virtual machine creates an instance of the java.lang.Class class to represent the type, which is placed in the heap area in memory, while the loaded type information is in the method area, just like all other objects.

Before loading a type, the class loading subsystem not only needs to locate and import the corresponding binary class file, but also verifies the correctness of the imported class, allocates and initializes memory for class variables, and parses symbolic references as direct references. These actions are carried out strictly in the following order:

1) load-find and load type binary data

2) Connect-perform validation, preparation, and parsing (optional)

3) verify to ensure the correctness of the imported type

4) prepare to allocate memory for class variables and initialize them to default values

5) parsing converts symbolic references in types to direct applications

Method area

For each type loaded by the class loading subsystem, the virtual machine saves the following data to the method area:

Fully qualified name of the type

Fully qualified name of the type superclass (java.lang.Object does not have a superclass)

Whether a type is a class type or an interface type

Access modifier of type

Fully qualified ordered list of any direct hyperinterface

In addition to the above basic type information, the following information will be saved:

Constant pool of type

Field information (including field name, field type, field modifier)

Method information (including method name, return type, number and type of parameters, method modifiers, if the method is not abstract and local, the bytecode of the method, the Operand stack, and the size and exception table of the local variable area in the frame of the method stack are also saved)

All class variables except constants (actually static variables of the class, because static variables are shared by all instances and are directly related to the type, so they are class-level variables that are saved in the method area as members of the class)

A reference to class ClassLoader

/ / the ClassLoader reference that was saved just now is returned.

String.class.getClassLoader ()

A reference to the Class class

/ / A reference to the Class class that you just saved will be returned

String.class

Note that the method area can also be recycled by the garbage collector.

Heap

All class instances or arrays created by Java programs at run time are placed in the same heap, and each Java virtual machine also has a pair of space, and all threads share a heap (which is why a multi-threaded Java program can cause object access synchronization problems).

Since each Java virtual machine has a different implementation of the virtual machine specification, we may not know how each Java virtual machine represents an object instance in the heap, but we can get a glimpse of this possible implementation:

Program counter

For running Java programs, each thread has its own PC (Program counter) register, which is created when the thread starts and is one word long to hold the location of the next line of code that needs to be executed.

Java stack

Each thread has a Java stack that stores the running state of the thread in stack frames. The virtual machine operates on the Java stack in two ways: pressing the stack and pulling the stack, both of which have been framed as units. The stack frame stores the incoming parameters, local variables, intermediate operation results and other data, which is popped up when the method is completed, and then released.

Take a look at the memory snapshot of the stack frame when two local variables are added together

Local method stack

This is where Java calls the operating system native library to implement JNI (Java Native Interface,Java native interface)

Executive engine

The core of the Java virtual machine, which controls the loading and parsing of Java bytecodes; for running Java programs, each thread is an instance of a separate virtual machine execution engine, either executing bytecode or executing local methods from the beginning to the end of the thread life cycle.

Local interface

The local method stack and operating system library are connected.

Note: all references to "Java virtual machine" refer to the "Java virtual machine specification for JavaEE and JavaSE platforms".

At this point, the study on "what is the architecture and memory model of the Java virtual machine" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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