In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the prototype and working principle of Java memory, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Mainly through the analysis of Java memory allocation stack, heap and constant pool to explain its working principle in detail.
1. Java virtual machine memory prototype
Register: we can not control the stack in the program: store basic types of data and object references, but the object itself is not stored in the stack, but stored in the heap: stored with new generated data static domain: stored in the object using static defined static member constant pool: storage constant non-RAM storage: hard disk and other * * storage space.
Constant pool (constant pool)
Constant pool means that it is determined at compile time and saved in the compiled. Some data in the class file. In addition to constant values (final) that contain various basic types (such as int, long, and so on) and object types (such as String and arrays) defined in the code, there are also some symbolic references in the form of text, such as:
1. Fully qualified names of classes and interfaces
2. Name and descriptor of the field
3. Method and name and descriptor.
The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used by this type, including direct constants (string,integer and floating point constants) and symbolic references to other types, fields, and methods. For the String constant, its value is in the constant pool. The constant pool in JVM exists in the form of a table in memory. For String type, there is a fixed-length CONSTANT_String_info table used to store text string values. Note: this table only stores text string values, not symbol references. At this point, you should have a clearer understanding of where the string values in the constant pool are stored. When the program is executed, the constant pool is stored in the Method Area, not in the heap.
3. Stack in Java memory allocation
The basic unit of the stack is the frame (or stack frame): whenever a java thread is running, the java virtual machine allocates a Java stack to that thread. When executing a java method, the thread pushes a frame into the java stack, which is used to store parameters, local variables, operands, intermediate results, and so on. When this method is finished, the frame pops up from the stack. All the data on the Java stack is private, and no other thread can have stack data for that thread. Some basic types of variable data and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates memory space for the variable on the stack. When the variable exits the scope, Java automatically frees up the memory space allocated for the variable, which can be immediately used for other purposes.
Heap in Java memory allocation
The heap in the java virtual machine is used to hold objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collection mechanism of the Java virtual machine. To put it simply, as opposed to stack, heap is mainly used to store java objects, and stack is mainly used to store object references. After generating an array or object in the heap, you can also define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, and the variable in the stack becomes a reference variable to the array or object. A reference variable is the equivalent of a name for an array or object, and you can later use the reference variable in the stack to access the array or object in the heap. A reference variable is equivalent to a name for an array or object.
Reference variables are ordinary variables that are assigned on the stack when defined, and are released after the program runs outside its scope. While the array and object itself are allocated in the heap, even if the program runs outside the code block where the statement that uses new to generate the array or object is located, the memory occupied by the array and object itself will not be freed. The array and object will only become garbage when there is no reference variable pointing to it, and can not be used, but still occupy the memory space. It is collected (released) by the garbage collector at a later uncertain time. This is why Java takes up more memory. In fact, variables in the stack point to variables in heap memory, which is the pointer in Java!
Java's heap is a run-time data area from which objects of the class allocate space. These objects are established by instructions such as new, newarray, anewarray, and multianewarray, and they do not require program code to be explicitly released. The heap is responsible for garbage collection, and the advantage of the heap is that it can allocate memory dynamically, and the lifetime does not have to tell the compiler in advance, because it allocates memory dynamically at run time, and Java's garbage collector automatically collects the data that is no longer in use. However, the disadvantage is that the access speed is slow due to the dynamic allocation of memory at run time.
The advantage of stack is that access speed is faster than heap, second only to registers, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be determined and lack of flexibility. The stack mainly stores some basic types of variable data (int, short, long, byte, float, double, boolean, char) and object handles (references).
A very important particularity of the stack is that the data in the stack can be shared. Suppose we also define:
The compiler processes int axiom 3 first; it first creates a reference with the variable an on the stack, and then looks for the value of 3 in the stack. If it doesn't find it, it stores 3 and points a to 3. Then process int b = 3; after creating the reference variable of b, because there is already a value of 3 in the stack, point b directly to 3. In this way, both an and b point to 3 at the same time.
At this point, the compiler will re-search if there is a value of 4 in the stack, if not, store 4 and point a to 4; if it already has, point a directly to this address. Therefore, the change of a value will not affect the value of b.
It should be noted that this kind of data sharing is different from the sharing in which references to two objects point to the same object at the same time, because the modification of a does not affect b, it is done by the compiler, and it helps to save space. One object reference variable modifies the internal state of the object, which affects another object reference variable.
On the prototype of Java memory and how it works is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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.
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.