In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample analysis of the run-time data area of the java virtual machine", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of the run-time data area of the java virtual machine".
JVMmemorymodel
The runtime data area (RuntimeDataAreas) described in the JVM specification. These areas are designed to store data that is used by JVM itself or by programs running on JVM.
Let's start with an overview of JVM, then introduce bytecodes, and finally introduce different data regions.
Overview
As an abstraction of the operating system, JVM ensures that the same code behaves consistently on different hardware or operating systems.
For example:
For the base type int, it is a 32-bit signed integer whether on a 16-bit / 32-bit / 64-bit operating system. Range from-2 ^ 31 to 2 ^ 31-1
Whether the operating system or hardware is large byte order or small byte order, make sure that the data in the memory stored and used by JVM is in large byte order (read high byte first)
Different JVM implementations may differ, but they are generally the same.
The picture above is an overview of JVM.
JVM interprets the bytecode generated by the compiler. Although JVM is the abbreviation of Java virtual machine, any language that can be compiled into bytecode can be run based on JVM, such as scala, groovyvcD4NCjxwPs6qwcux3MPixrW3sbXEtMXFzEkvT6Os19a92sLru+Gxu2NsYXNzbG9hZGVyvNPU2LKiu7q05rW91MvQ0Mqxyv2+3cf41tC1xNK7uPbH+NPyo6zWqrXAvNPU2Mv8tcRjbGFzc2xvYWRlcrG7z/q72bvy1d9KVk3No9a51MvQ0KGjPC9wPg0KPHA+vNPU2LXE19a92sLrzai5/da00NDS/cfmKGV4ZWN1dGlvbiBlbmdpbmUpvfjQ0L3iys26zda00NA8L3A+DQo8cD7WtNDQ0v3H5tDo0qq05rSis8zQ8snPz8LOxKOsscjI57PM0PLWtNDQtb3ExNK70NCjrLvy1d/K/b7dvMbL47XE1tC85L3hufs8L3A+DQo8cD7WtNDQ0v3H5tKyuLrU8LSmwO3T67XXsuOy2df3z7XNs7XEvbu7pTwvcD4NCjxwPioquty24EpWTba8yrXP1sHLvLTKsbHg0uu5psTcKEpJVD1qdXN0IGluIHRpbWUpoaNKSVS+zcrHsNG+rbOj1rTQ0LXEtPrC6yjIyLXjtPrC6ymx4NLrs8mxvrXYtPrC6yhOYXRpdmUgQ29kZSmho7Tmt8VKSVSx4NLryfqzybT6wuu1xMf40/Kzxs6qPC9wPg0KPHA+tPrC67u6tObH+ChDb2RlIENhY2gpoaO8tMqxseDS67y8yvUoSklUKby2tPO1xMzhuN/By0pWTbXE0NTE3CoqPC9wPg0KPGgyIGlkPQ== "stack stack-based architecture" > stack-based architecture.
JVM uses a stack-based architecture. Although the stack is transparent to the developer, the stack has an important role or influence on the generated bytecode and JVM.
The program we developed will convert low-level operations and store them in bytecode. Mapping to operation instructions through operands (operand) in JVM. According to the JVM specification, the parameters required by the operation instruction are obtained from the Operand stack (the operand stack).
Give an example of the addition of two numbers. This operation is called iadd. The following is the process of 3-4 in a bytecode
First push 3 and 4 into the Operand stack
Call iadd instruction
The iadd instruction pops up 2 numbers from the top of the Operand stack
The result of 3x4 is pressed into the Operand stack for later use.
This approach is called stack-based architecture. There are other ways to handle low-level operations, such as register-based architecture (register based architecture).
Bytecode
Java bytecode is the result of the conversion of java source code into a series of low-level operations. Each operation consists of an opcode (opcode or operation code) of one byte length and parameters of zero or more bytes (but the parameters used by most operations are obtained through the Operand stack). A byte can represent 256 numbers, from 0x00 to 0xff, and currently to java8, a total of 204 are used.
The following lists different kinds of bytecode opcodes as well as their scope and simple description
Constants: push the value of the constant pool or known value into the Operand stack. 0x00-0x14
Loads: pushes the values of local variables into the Operand stack. 0x15-0x35
Stores: assign the load value from the Operand stack to the local variable 0x36-0x56
Stack: processing Operand stack 0x57-0x5f
Math: getting values from the Operand stack for basic mathematical calculations 0x60-0x84
Conversions: converting between types 0x85-0x 93
Comaprisons: the comparison operation 0x94-0xa6 of two values
Controls: perform control operations such as goto, return, loops, etc. 0xa7-0xb1
References: executes an allocation object or array to get or check references to objects, methods, or static methods. You can also call static methods. 0xb2-oxc3
Extended: Extended: operations from the others categories that were added after. From value 0xc4 to 0xc9
I don't know what this sentence means..)
Reserved: the internal slot of JVM implementation is 0xca.oxfeoxff.
These 204 operations are very simple, to give a few examples
Ifeq (0x99) determines whether two values are equal.
Iadd (0x60) adds two numbers
I2l (0x85) transforms an int into long
Arraylength (0xbe) returns the length of the array
Pop (0x57) pops a value from the top of the Operand stack
We need a compiler to create bytecode files, and the standard java compiler is javac in jdk.
Public class Test {public static void main (String [] args) {int a = 1; int b = 15; int result = add (int b);} public static int add (int a, int b) {int result = a + b; return result;}}
The bytecode file of "Test.class" can be obtained through "javac Test.java". The bytecode file is binary. We can convert the binary bytecode file into text form through javap.
Java-verbose Test.class
Classfile / C:/TMP/Test.class Last modified 1 avr. 2015 Size 367bytes MD5 checksum adb9ff75f12fc6ce1cdde22a9c4c7426 Compiled from "Test.java" public class com.codinggeek.jvm.Test SourceFile: "Test.java" minor version: 0 major version: 51 flags: ACC_PUBLIC ACC_SUPERConstant pool: # 1 = Methodref # 4.room15 / / java/lang/Object. "": () V # 2 = Methodref # 3.room16 / / com/codinggeek/jvm/Test.add: (II) I # 3 = Class # 17 / / com/codinggeek/jvm/Test # 4 = Class # 18 / / java/lang/Object # 5 = Utf8 # 6 = Utf8 V # 7 = Utf8 Code # 8 = Utf8 LineNumberTable # 9 = Utf8 main # 10 = Utf8 ([Ljava/lang/String ) V # 11 = Utf8 add # 12 = Utf8 (II) I # 13 = Utf8 SourceFile # 14 = Utf8 Test.java # 15 = NameAndType # 5 public com.codinggeek.jvm.Test 6 / "": () V # 16 = NameAndType # 11 / add: (II) I # 17 = Utf8 com/codinggeek/jvm/Test # 18 = Utf8 java/lang/Object {public com.codinggeek.jvm.Test () Flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial # 1 / / Method java/lang/Object. "": () V 4: return LineNumberTable: line 3: 0 public static void main (java.lang.String []) Flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=4, args_size=1 0: iconst_1 1: istore_1 2: bipush 15 4: istore_2 5: iload_1 6: iload_2 7: invokestatic # 2 / / Method add: (II) I 10: istore_3 11: return LineNumberTable: line 6: 0 line 7: 2 line 8: 5 line 9: 11 public static int add (int, int) Flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=3, args_size=2 0: iload_0 1: iload_1 2: iadd 3: istore_2 4: iload_2 5: ireturn LineNumberTable: line 12: 0 line 13: 4}
You can see that bytecode is not just a simple translation of java code, it includes:
Constant pool (cosntant pool) description of the class. A constant pool is an JVM data area used to store class metadata, such as method names, parameter lists, and so on within the class. When JVM loads a class, the metadata is loaded into the constant pool
The specific location information of the function and Tmall's variables in the bytecode is provided through the line number table and or the local variable table.
Translation of java code (including hidden parent class constructs)
Provide more specific operations on Operand stacks and more complete ways to pass and get parameters
Here is a simple description of the information stored in a bytecode file
ClassFile {u4 magic; U2 minor_version; U2 major_version; U2 constant_pool_count; cp_info constant_ Pool [constant _ pool_count-1]; U2 access_flags; U2 this_class; U2 super_class; U2 interfaces_count; U2 interfaces [interfaces _ count]; U2 fields_count; field_info fields [fields _ count]; U2 methods_count; method_info methods [methods _ count]; U2 attributes_count; attribute_info attributes [attributes _ count];}
Runtime data area
The runtime data area is the design of the memory area in which the data is stored. This data can be used internally by developers or JVM.
Heap (Heap)
The heap is created when the JVM starts and is shared by all JVM threads. All class instances and arrays are assigned to the heap (created by new).
The heap must be managed by a garbage collector, which is responsible for releasing objects created by the developer and will no longer be used.
The strategy for garbage collection is determined by the JVM implementation (for example, HotSpot provides a variety of algorithms).
There is a maximum value limit in heap memory. If this value is exceeded, JVM will throw an OutOfMemroy exception.
Method area (Method area)
The method area is also shared by all threads in JVM. The same is created with JVM startup. The data stored in the method area is loaded from the bytecode by classloader, and the data will be consistent during the run of the application, unless the classloader that loaded them is destroyed or the JVM stops.
The method area stores the following data:
Class information (property name, method name, parent class name, borrowed name, version, etc.)
Method and constructed bytecode
Run-time pool created when each class is loaded
The JVM specification does not force the implementation of method zones in the heap. Prior to java7, HotSpot used a zone called permanent band (PermGen) to implement the method zone. Permanent belt is adjacent to the heap (memory management is the same as the heap), and the default bit is 64MB
Starting with java8, HptSpot uses separate local memory to implement the method area, named the metadata area (Metaspace). The maximum available space in the metadata area is the available memory for the entire system.
If the method does not apply for available memory, JVM will also throw OutOfMemoryError.
Run time Pool (Runtime constant pool)
The run-time pool is part of the method area. Because of the importance of running constant pools for metadata, it is described separately in the java specification outside the method area. The runtime pool grows with the loaded classes and interfaces.
Constant pools are a bit like grammar tables in traditional languages. In other words, when a class, method, or property is called, JVM runs the constant pool to find the real address of the data in memory. The runtime pool also contains string literals or basic types of constants.
Stirng myString= "This is a string litteral" static final int MY_CONSTANT = 2
Pc (program counter) register (per thread) The pc Register (Per Thread)
Each thread has its own pc (Program counter) register, which is created together with thread creation. Each thread can execute only one method at a point in time, called the thread's current method (current method). The pc register contains the address of the instruction that JVM is currently executing (in the method area).
If the currently executed method is a local method (native), the value of the pc register is undefined
Virtual machine stack per thread-java-virtual-machine-stacks-per-thread "> virtual machine stack (per thread) java virtual machine stacks (per thread)
The virtual machine stack stores multiple frames, so before describing the stack, let's look at the next frame
Frame (frames)
A frame is a data structure that contains multiple pieces of data that represent the current state of the method being executed by the thread:
Operand stack (Operand Stack): as mentioned earlier, bytecode instructions use Operand stacks to pass parameters
Array of local variables (Local variable array): this array contains all local variables within a scope of the current execution method. This array can contain primitive types, references, or return addresses. The size of the array of local variables is determined at compile time. Jvm uses local variables to pass parameters when a method is called, and an array of local variables of the called method is created through the Operand stack of the calling method.
Runtime constant pool reference: the constant pool that refers to the currently executed method of the current class. JVM uses constant pool references to signal to real memory references.
Stack (stack)
Each JVM thread has a private JVM stack that is created at the same time as the thread. The java virtual machine stack stores frames. Each time a method is called, a frame is created and pushed into the virtual machine stack. When the method execution is complete, the frame is also destroyed (whether the method completes normally or throws an exception)
Only one frame is available during a thread's execution. This frame is called current frame (current frame).
Operations on local variables and Operand stacks are usually accompanied by references to the current frame.
Let's look at another example of addition.
Public int add (int a, int b) {return a + b;} public void functionA () {/ / some code without function call int result = add (2mai 3); / / call to function Bhand / some code without function call}
Inside method A, frame An is the current frame and is located at the top of the virtual machine stack. At the beginning of the call to the add method, create a new frame B and press it into the virtual machine stack. Frame B becomes the new current frame.
The array of local variables of frame B is populated by the data in the Operand stack of frame A. When the add method ends at, frame B is destroyed and frame A becomes the current frame again. The result of the add method is pressed into the Operand stack of frame A, so that method A can obtain the result of add through the Operand stack of frame A.
The above is all the contents of the article "sample Analysis of the Runtime data Zone of the java Virtual Machine". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.