Java's runtime data storage mechanism

Source: Internet
Author: User



Original address: http://yanwushu.sinaapp.com/java_data_storage/


Java The program needs to allocate memory for a series of values or objects at run time, where do these values exist? What data structures are used for storage? What are the characteristics of these data structures? This paper attempts to illustrate the fur of this proposition.


Concept

for Java, there are six different concepts for data storage, and they are:

1. Register (register), is the fastest storage area, located inside the processor. Because the number of registers is extremely limited, the registers are allocated by the compiler on demand. Programmers cannot use Java code to consume storage space in registers, or, at the level of Java development, the operation of registers has been encapsulated.

2. Stack, located in Universal RAM. Fast access, second only to registers. If the stack pointer moves down, the new memory is allocated, and if you move up, the memory is freed. When you create a program, the JAVA compiler must know the exact size and lifecycle of all the data stored in the stack, because it must generate the appropriate code to move the stack pointer up and down to allocate and free memory. Because of this storage feature of the stack, some data exists in the stack, such as object references and variable values of underlying types, but some data is not suitable for storage on the stack, such as an instance of an object.

3. Heap, a run-time data area located in RAM. The space in the heap is dynamically allocated, so it is not necessary to know the size and life cycle of the data. Therefore, there is great flexibility in storing data in the heap. Instances and arrays of Java objects are placed here. Expired objects in the heap are reclaimed by the GC. The heap has a slower access speed.

4. static storage (storage), a fixed position in RAM. Stores static data that is decorated with the static keyword in the program.

5. constant Storage (constant storage), constant values are usually stored directly inside the program code, so it is safe to do so because they will never be changed.

6. non-RAM storage. If the data is completely outside of the program, it can be free from any control of the program and can exist when the program is not running.


Data sharing for stacks and static storage

In one case, the hypothesis is defined as:

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 finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in and then 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. In this case, 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.

Data sharing is the same for static data.

Java's runtime data storage mechanism

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.