Data storage in Java

Source: Internet
Author: User

1. Register (register ). This is the fastest storage zone, because it is located in a different place from other storage areas-inside the processor. However, the number of registers is extremely limited, so the registers are allocated by the compiler as needed. You cannot directly control or Program Any indication that the register exists.
2. Stack ). It is located in general-purpose RAM (Random Access Memory), but its "Stack pointer" can be directly supported from the processor. If the stack pointer moves down, new memory is allocated. If the stack pointer moves up, the memory is released. This is a fast and effective distribution storage method, second only to registers. 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 or down. This constraint limits the flexibility of the program, so although some java data is stored in the stack-especially object reference, Java objects are not stored in it.
3. Heap ). A universal memory pool (also exists in the ram zone) used to store all Java objects. The benefit of heap is that the compiler does not need to know how many storage areas are allocated from the heap or how long the stored data remains in the heap. Therefore, it is flexible to allocate storage in the heap. When you need to create an object, you only need to use new to write a simple line of code. When this line of code is executed, it will be automatically stored and allocated in the heap. Of course, there must be a price for this flexibility. It takes more time to store objects by Heap Storage and sharding ratio by stack (if it is true that the objects can be saved by stack in Java like in C ++ ).
4. static storage ). Here "static" refers to "in a fixed position" (although also in Ram ). Static storage stores the data that persists when the program is running. You can use the keyword static to identify the specific elements of an object as static, but Java objects are never stored in static buckets.
5. Constant storage ). Constant values are usually directly stored in the program code. This is safe because they will never be changed. Sometimes, in an embedded system, constants are isolated from other parts, so in this case, you can choose to store them in ROM (read-only memory.
6. Non-ram storage ). If the data exists completely outside of the program, it can exist without any control of the program. Two basic examples are "streamed object" and "persistence object )". In a stream object, an object is converted into a word throttling, which is usually sent to another machine. In persistent objects, objects are stored on disks, so they can remain in their own State even if the program is terminated. The trick of this storage method is to convert objects into things that can be stored on other media and restore them to conventional, Ram-based objects as needed.



1. Register)
In existence and CPU, the fastest access speed, but a limited number


2. Stack (stack and stack)
In existence and memory, the speed and efficiency of access are second only to register. When a program is loaded, the compiler must accurately know the size of all the required space and operate through pointers.


3. Heap (HEAP)
In memory and memory, the access method is flexible and requires no compiler concern. The speed is not as high as that of stack.


4. Static (static type)
The entire program can be accessed. The object attributes can be static, but Java objects won't be static.


5. Constant (constant)
It exists in the program code and cannot be changed


6. non-volatile data
A. stream type data
B. persistent data



In terms of speed, there are the following relationships:
Register <stack
"Thinking in Java" mentioned above

Here, we mainly want to talk about the relationship between the stack and the stack:

Heap: heap is a so-called dynamic memory. It can be recycled when there is no need in the heap to allocate new memory requests. The data in the heap memory is unordered, that is, the first allocation and the subsequent allocation of memory are not necessarily in the relationship between locations, and there can be no sequential release. Generally, the user can allocate resources freely. The malloc allocates the heap and needs to be released manually.

STACK: Stack. In fact, there is only one inbound queue, that is, the first in last out (first in last out), the first allocated memory must be released. Generally, it is automatically allocated by the system to store function parameter values, local variables, and so on. It is automatically cleared.

In addition, the heap is global, and the stack is a small block when every function enters. When the function returns, the static and global variables are released, and the new variables are all placed in the heap, the local variables are placed in the stack, so the function returns and the local variables are all gone.

In practice, stacks are used to store method calls. However, it is used for Object Storage.

The basic types in Java need special treatment. In Java, objects created through new are stored in the "heap". Therefore, using new to create a small and simple variable, such as the basic type, is often not very effective. Therefore, Java uses the same method as C and C ++ for these types. That is to say, you do not need to create a new variable, but create an "automatic" variable that is not "referenced. This variable has its "value" and is placed in the stack, so it is more efficient.

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.