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 prototype of java virtual machine memory

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

Share

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

This article focuses on "what is the prototype of java virtual machine memory". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the prototype of java virtual machine memory?"

What is the memory prototype of the java virtual machine

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: permanent storage space such as hard disk.

Constant pool (constantpool)

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

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 floatingpoint 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 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 MethodArea, not in the heap.

How does Java memory work?

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 in the program 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. The array and the object itself are allocated in the heap, even if the program runs outside the block of code 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 released, the array and object will become garbage when there is no reference variable pointing to it, can not be used, but still occupy the memory space, and will be collected (released) by the garbage collector at an uncertain time thereafter. 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 inta=3;intb=3; compiler first processes inta=3;. First, it creates a reference in the stack with the variable a, and then looks up whether there is a value of 3 in the stack. If it cannot find it, it stores 3 and points a to 3. Then after intb=3; creates the reference variable of b, because there is a value of 3 in the stack, it points b directly to 3. 0. 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.

At this point, I believe you have a deeper understanding of "what is the prototype of java virtual machine memory". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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