Analysis of JVM Memory Model and javajvm Model Based on Java Technology

Source: Internet
Author: User

Analysis of JVM Memory Model and javajvm Model Based on Java Technology

1. JVM Memory Model

The JVM specification divides the VM memory into heap, stack, method zone, local method stack, and program counter. The heap and method areas are the areas shared by threads. Local method stacks, stack memory, and program counters are exclusive to threads.

2. Stack

Stack is also called Stack memory, which is a space exclusive to the thread. Its life cycle ends with the end of the thread, and the Stack memory is released. When each thread calls a method, a Stack Frame is created, when a method is called, it is a stack frame's inbound stack process. When the method is executed and exited, the stack frame will also go out of the stack. Stack frames follow the "advanced post-release" principle.

Stack frame data:

Local variable: input/output parameters and variables in the Method

Stack frame operation: records the stack and inbound stack records

Stack frame data: Includes class files and methods.

Exception:

If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, the Java Virtual Machine will throw an StackOverflowError exception.

If the virtual machine can be dynamically expanded, but you cannot apply for enough memory to create an initial java stack for the new thread, an OutOfMemoryError is thrown.

 

3. Heap

The largest memory block in the Java VM During Heap. The heap mainly stores the objects and arrays created with the new keyword. Java developers do not need to allocate memory space for objects or release memory, which reduces the difficulty of C/C ++ development and memory overflow leakage. The objects in the Java heap are released by using the JVM memory-managed garbage collector to recycle GC, which uses the generational collection algorithm. Therefore, the heap memory can be further divided into permanent, new, and old-age areas.

Permanent storage zone: stores the class interface metadata carried by JDK. The data loaded to this zone is not recycled by the garbage collector. When the virtual machine is disabled, the memory is released.

New-born zone: the new-born zone is the birth, growth, and extinction of the class. The new-born zone is also divided into Eden, surviving zone From Region vor 0, To region vor 1. When the Eden area is full, the garbage collector is started to destroy objects not referenced in the area, and then the remaining objects are moved to the surviving 0 area. If the surviving 0 area is full, then move to the surviving Zone 1. If the surviving Zone 1 is full, move to the old age.

In the old age, the remaining objects in the new area are stored. Generally, the object data in the pool is saved.

4. Program counters

The program counter is unique to each thread and serves as the row number indicator of the bytecode executed by the current thread. During the Java Virtual Machine execution process, the Java interpreter selects the next bytecode instruction by changing the value of the program counter.

Program counters between threads are independent.

If the thread is executing a Java method, the counter stores exactly the address of the Virtual Machine bytecode instruction being executed. If the thread executes a local method, the program counter value is null.

5. Local method Stack

In the Java System, the local method stack can execute non-java programming code and c/c ++. You only need to load the library file. dll/. so. In the HotSpot virtual machine, the virtual machine stack and the local method Stack are merged directly.

6. Method Area

The method area is a multi-threaded shared area that stores information about classes loaded by virtual machines, constants, static variables, and Code Compiled by the compiler in real time.

 


Detailed explanation of Java Virtual Machine?

1 Java technology and Java Virtual Machine
Speaking of Java, people first think of Java programming language. However, Java is actually a technology, which consists of four aspects: java programming language, Java file format, Java Virtual Machine and Java application interface (Java API ). Shows the relationship between them:

Figure 1 Relationship between four aspects of Java
The runtime environment represents the Java platform. Developers compile Java code (. java file) and compile it into bytecode (. class file ). The final bytecode is loaded into the memory. Once the bytecode enters the virtual machine, it will be interpreted and executed by the interpreter, or converted to the machine code by the real-time code generator. We can also see that the Java platform is built by the Java Virtual Machine and Java application interface, and the Java language is the channel to this platform. programs compiled and compiled in Java can run on this platform. Shows the structure of the Platform:

In the structure of the Java platform, it can be seen that the Java Virtual Machine (JVM) is at the core position and is the key for programs to be irrelevant to the underlying operating system and hardware. Below it is A porting interface, which consists of an adapter and a Java operating system. The platform-dependent part is called an adapter; JVM is implemented on a specific platform and operating system by porting interfaces. Above the JVM is the basic Java class library, extended class library, and their APIs, applications and applets compiled using Java APIs can run on any Java platform without considering the underlying platform) java platform independence is achieved by separating programs from operating systems.
So what is a Java Virtual Machine (JVM? When we talk about JVM, we may mean:
Abstract description of JVM specifications;

Specific implementation of JVM;

A jvm instance generated during the running of the program.
Abstract descriptions of JVM specifications are a collection of concepts that have been described in detail in The book The Java Virtual Machine Specification (Java Virtual Machine specifications; the specific implementation of JVM is either software or a combination of software and hardware, which has been implemented by many manufacturers and coexist on multiple platforms; java program running tasks are undertaken by a single JVM runtime instance. The Java Virtual Machine (JVM) discussed in this article is mainly applicable to the third scenario. It can be regarded as an imaginary machine, which is realized through software simulation on an actual computer. It has its own imaginary hardware, such as the processor, stack, and register, you can also use your own command system.
JVM has a clear task in its life cycle, that is, to run Java programs. Therefore, when a Java program is started, an instance of JVM is generated. When the program runs, the instance also disappears. Next we will conduct a more in-depth study on the JVM architecture and its running process.
2 architecture of Java Virtual Machine
As mentioned earlier, JVM can be implemented by different vendors. Due to vendor differences, the implementation of JVM is different. However, JVM can still implement cross-platform features, thanks to the architecture during JVM design.
We know that the behavior of a JVM instance is not only about itself, but also about its subsystems, storage areas, data types, and commands, they describe an abstract internal architecture of JVM. The purpose is not only to specify the internal architecture when JVM is implemented, but also to provide a way, it is used to strictly define external behaviors during implementation. Each JVM has two mechanisms: one is to load a class (class or interface) with the appropriate name, called the class loading subsystem; the other one is responsible for executing commands contained in loaded classes or interfaces, called the running engine. Each JVM also includes five parts: Method Area, heap, Java stack, program counter, and local method stack. The architecture of these parts is composed of the class loader and running engine mechanism:
... The remaining full text>


Java's constant pool contains some things.

Understanding Java constant pool
The memory model of the JVM runtime data zone consists of five parts:

[1] Method Area
[2] heap
[3] JAVA stack
[4] PC register
[5] local method Stack

For String s = "haha", its VM commands:
0: ldc #16; // String haha
2: astore_1
3: return

For the above VM commands, their respective instructions are described in "deep into Java Virtual Machine" (combined with the above instance ):

Ldc Command Format: ldc, index

Ldc instruction process:

To execute the ldc command, JVM first searches for the constant pool entry specified by index. In the constant pool entry pointed to by index, JVM searches for CONSTANT_Integer_info, CONSTANT_Float_info, and CONSTANT_String_info. If these entries are not available, JVM will parse them. For the preceding hahaJVM, The CONSTANT_String_info entry is found, and the reference pointing to the detained String object (generated by the process that parses the entry) is pushed to the operand stack.

Astore_1 Command Format: astore_1

Astore_1 command process:

To execute the astore_1 command, JVM pops up a reference type or returnAddress type value from the top of the operand stack, and then stores the value in a local variable specified by index 1, save the reference type or returnAddress type value to local variable 1.

Return command process:

Return from the method. The return value is void.

My personal understanding:

From the execution process of the preceding ldc command, we can conclude that the value of s is a reference from the detained String object (generated by the process that parses the entry, that is, it can be understood that it is copied from the reference of the detained String object, so my personal understanding is that the value of s exists in the stack. The above is an analysis of the s value, followed by an analysis of the "haha" value. We know that, for String s = "haha" where "haha" is determined during the JAVA program compilation period. To put it simply, the value of haha is after the program is compiled into a class file, it is generated in the class file (you can use the UE editor or other text editing tools to view the haha value in the bytecode file after opening the class file ). During JAVA program execution, the first step is to generate the class file and then load it To the memory for execution by JVM. In this case, the JVM loads this class into the memory. In this case, how does one open up space for the class and store the haha value in the memory?

Here, let's take a look at the structure of the JVM constant pool, which is described in the deep Java Virtual Machine book:

Constant pool

The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used for this type, including direct constants (string, integer, and floating point constants) and symbolic references to other types, fields, and methods. For a String constant, its value is in the constant pool. The constant pool in JVM exists in the memory as a table. For the String type, there is a fixed-length CONSTANT_String_info table to store the text String value. Note: this table only stores text string values, but does not store symbol references. Here, we should have a clear understanding of the storage location of string values in the constant pool.

After introducing the concept of JVM constant pool, let's talk about the memory distribution position of the "haha" value. The haha value is actually before the class file is loaded into the memory by JVM and the engine parses the ldc command and executes the ldc command, JVM is already the CON of the haha string in the constant pool ...... remaining full text>
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.