Java runtime Data Storage Mechanism

Source: Internet
Author: User

Java runtime Data Storage Mechanism

 

 

When a Java program is running, it needs to allocate memory for a series of values or objects. Where are these values? What kind of data structure is used for storage? What are the characteristics of these data structures? This article attempts to illustrate the fur of this proposition.

 

Concept

For Java, there are six different concepts for data storage:

1. Registers are the fastest storage zone located inside the processor. The number of registers is extremely limited, so the registers are allocated by the compiler as needed. Programmers cannot use Java code to use the storage space in registers, or the register operations have been encapsulated at the Java development level.

2. stack, which is located in universal RAM. Fast access speed, second only to registers. If the stack pointer moves down, new memory is allocated. If it moves up, the memory is released. When creating a program, the JAVA compiler must know the exact size and lifecycle of all data stored in the stack, because it must generate the corresponding code to move the stack pointer up and down, and then allocate and release the memory. Due to this storage feature of the stack, some data exists in the stack, such as the object reference and the variable value of the basic type. However, some data is not suitable for storing in the stack, such as the object instance.

3. heap, a runtime data zone located in RAM. The space in the heap is dynamically allocated, so you do not need to know the data size and lifecycle. Therefore, it is flexible to store data in the heap. The Java object instance and array are placed here. GC is responsible for collecting expired objects in the heap. The access speed of the heap is slow.

4. static storage: a fixed block in RAM. Stores static data, which is modified using the static keyword in the program.

5. constant storage. constant values are usually directly stored in the program code. This is safe because they will never be changed.

6. Non-RAM storage. If the data exists completely outside of the program, it can exist without any control of the program.

 

Stack and static storage data sharing

Let's use a case to understand the following definitions:

Int a = 3;

Int B = 3;

The compiler first processes int a = 3. First, it creates a reference with the variable a in the stack, and then finds whether the value 3 in the stack exists. If no value is found, store 3 and point a to 3. Then process int B = 3. After the referenced variable of B is created, B is directed to 3 because there is already 3 in the stack. In this way, both a and B point to 3 at the same time. At this time, if a is set to 4 again, the compiler will re-search whether there are 4 values in the stack. If not, it will store 4 and make a point to 4; if yes, direct a to this address. Therefore, changing the value of a does not affect the value of B. Note that the sharing of data is different from the sharing of two objects pointing to one object at the same time, because the modification of a does not affect B, which is completed by the compiler, it facilitates space saving. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.

Data sharing is the same for static data.

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.