Java Memory Prototyping Analysis

Source: Internet
Author: User

Java Virtual Machine Memory prototype

Register: cannot be controlled in the program

Stack: A reference to a primitive type of data and objects, but the object itself is not stored in the stack, but is stored in the heap

Heap: Storing data generated with new

Static domain: Static members that are stored in the object with static definitions

Chang: Storing variables

Non-RAM storage: Persistent storage space such as hard drives

Stacks in Java memory allocation

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 to the thread that executes a Java method, pressing a frame into the Java stack, which is used to store parameters, local variables, operands, intermediate 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 use the thread's data.

Some basic types of variable data and object reference variables defined in the function are allocated on the stack memory of the function.

When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the variable exits the scope, Java automatically frees the memory space allocated for that variable, which can be used immediately for other purposes.

Heap of Java Memory allocations

The heap in a 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 manage the heap's memory.

Simple say and Stack, the heap is mainly used to store Java objects, the stack is mainly used to store object references, in the heap after an array or an object has been generated, you can also define a special variable in the stack, so that the value of this variable in the stack is equal to the array or object in the heap memory of the first address, This variable in the stack becomes the reference variable for 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 is run outside the 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 the object's statement, and the array and the object become garbage when no reference variable points to it, and are no longer used, but still occupy memory space. At a later indeterminate time the garbage collector (released). 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 the objects of the class allocate space. These objects are established through directives such as New,newarray,anewarray and Multianewarray, and they do not require program code to display the release. 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 the runtime dynamically allocated memory, 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 handles (references).

The stack has a very important particularity, that is, the data stored 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 a, and then looks for a value of 3 in the stack, and if not found, stores 3 in and points a to 3. Then the int b=3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. This occurs when a A, a, and a 3 are pointed. 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.

Constant pool

Chang refers to some data that is determined at compile time and is saved in the compiled. class file. Constant values (final) that deal with the various basic types defined in the code (such as Int,long, etc.) and object types (such as strings and arrays) also contain symbolic references that appear as text, such as:

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

2. Name and descriptor of the field;

3. Name and descriptor of the method;

The virtual machine must maintain a constant pool for each type that is mounted. A constant pool is an ordered collection 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.

When the program executes, the constant pool is stored in the method area, not the heap.

Method area

The method area, like a heap, is a zone that is shared by threads. In the method area, information is stored for each class (including the name of the class, method information, field information), static variables, constants, and compiler-compiled code.

In the JVM specification, there is no mandatory requirement for the method area to implement garbage collection. Many people are accustomed to calling the method area "permanent generation" because the hotspot virtual machine implements the method area as a permanent generation, so that the JVM's garbage collector can manage this part of the area as if it were managed by the heap, eliminating the need to specifically design a garbage collection mechanism for this part. Since JDK7, however, the hotspot virtual machine has removed the run-time constant pool from the permanent generation.

Java Memory Prototyping Analysis

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.