"Go" Java Virtual machine memory prototype

Source: Internet
Author: User

This paper mainly analyzes the Java memory allocation stack, heap and Chang detailed explanation of its working principle.

One, Java Virtual machine memory prototype

Register: We have no control over the stack in the program: a reference to the basic type of data and objects, but the object itself is not stored in the stack, Instead, the heap is stored in the heap: the static domain of data generated with new: Static members stored in the object defined with static Chang: Store constant non-RAM storage: Persistent storage space such as hard disks.

Second, Chang (constant pool)

Chang refers to the compilation period that is determined and is saved in the compiled. Some data in the class file. In addition to the constant values (final) that contain the various basic types defined in the code (such as int, long, and so on) and object types (such as strings and arrays), there are also some symbolic references that appear as text, such as:

1. The fully qualified name of the class and interface;

2, the 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 that 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 a constant pool. The Chang in the JVM exists as a table in memory, and for string types there is a fixed-length constant_string_info table for storing literal string values, note that the table stores only literal string values and does not store symbol references. In this case, there should be a clearer understanding of where the string values in the constant pool should be stored. When the program executes, the constant pool is stored in the method area, not the heap.

Third, Java memory allocation in the stack

The base unit of the stack is the frame (or stack frame): Whenever a Java thread runs, the Java virtual opportunity allocates a Java stack for that thread. When the thread executes a Java method, it presses a frame into the Java stack, which is used to store parameters, local variables, operands, intermediate operation results, and so on. When this method executes, the frame pops up from the stack. All data on the Java stack is private and other threads cannot stack data for that thread. Some basic types of variable data and object reference variables that are defined in the function are allocated in the function's stack memory. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the variable exits the scope, Java automatically frees the memory space allocated for the variable, and the memory space is immediately available for another use.

Iv. Heap in Java memory allocation

The heap in the Java Virtual machine is used to hold the 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 manage the heap's memory. Relatively simple say and stack, the heap is mainly used to store Java objects, the stack is mainly used to store object references ... After generating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. A reference variable is a name that is an array or an object, and you can use reference variables from the stack to access the arrays or objects in the heap later in your program. A reference variable is equivalent to a name that is an array or an object.

A reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed when the program runs beyond the block of code that uses new to produce the array or object's statement, and the array and object become garbage when no reference variable points to it, not in use, but still occupy memory space. The garbage collector takes off (releases) at a later indeterminate time. This is why the Java comparison accounts for memory. In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in Java!

The Java heap is a run-time data area in which objects allocate space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variable data (int, short, long, byte, float, double, Boolean, char) and object handle (reference).

Stack has a very important particularity, is that there is data in the stack can be shared. Let's say we define both:

int a=3; int b=3; The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then looks for a value of 3 in the stack, if not found, stores 3 in it, and then points a to 3. int B = 3 is processed, and after the reference variable of B is created, since there is already 3 in the stack, Point B directly to 3. Thus, A and B both point to 3.

At this point, if you make a=4 again, then the compiler will re-search the stack for 4 values, if not, then store 4 in, and a point to 4; Therefore the change of a value does not affect the value of B.

It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of a does not affect B, which is done by the compiler, which facilitates space saving. An object reference variable modifies the internal state of the object, affecting another object reference variable.

"Go" Java Virtual machine memory prototype

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.