Java runtime data storage mechanism and java Data Storage Mechanism

Source: Internet
Author: User

Java runtime data storage mechanism and java Data Storage Mechanism



Address: http://yanwushu.sinaapp.com/java_data_storage/


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.


Memory Data storage mechanism?

1. register (register ). This is the fastest storage zone, and the number of registers is extremely limited. Therefore, you cannot directly control the number of registers allocated by the compiler as needed.

2. Stack ). Located in general RAM (random-access memory, random access memory), its "Stack pointer" can be obtained from the processor. Stack pointer

Next, a new memory is allocated. If you move up, the memory is released. When creating a program, the Java compiler must know the 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. Because of the constraints, Java object references and variables are generally stored.

Advantage: fast distribution of storage, second only to registers.
Disadvantage: limits the flexibility of the program.

3. heap ). Universal memory pool, used to store all Java objects. The advantage of heap is that the editor does not need to know how many storage areas should be allocated in the heap or the number of storage areas.

How long data remains in the heap. In Java, to create an object, you only need to use new. When this line of code is executed, storage is automatically allocated in the heap.

Advantage: allocate storage in the heap with great flexibility.
The disadvantage is that it takes more time to store data by heap than by stack.

4. static storage ). Is in a fixed position (also in RAM ). Static storage stores the data that persists when the program is running. It is usually a static variable of Java,

Java objects are never stored in static buckets.

5. constant storage ). It is usually stored in ROM (read-only memory) Because constants themselves will never be changed.

Storage of various types of data in java

I have been studying Java for many years and cannot answer this question carelessly. After referring to relevant knowledge, I have explained the following.
Java memory is allocated in three ways,
1. static storage zone: allocated when a program exists in it during compilation, such as static variables;
2. Stack zone: local variables of various original data types are created on the stack. When the program exits from the scope of the variable, the memory of the variable will be automatically released.
3. Heap area: objects (including arrays) are created in the heap. When the program is running, the new keyword is used to create the object. When the object is created, the memory is allocated to it in the heap.

Based on the above analysis, it is obvious that static declarations and data segments are stored in the static storage area, while constants are stored in the stack area. Hope to help you and everyone.

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.