In Java, the storage area is divided into 6 categories. Register (register), stack (stack), heap (heap), static storage (static storage), constant store (constant storage), and non-random access Store (Non-ram). 1. Register (Register). Unlike other stores, registers are located on the CPU, so it is the fastest storage area. But since the number of registers in the CPU is very limited, Therefore, the use of registers is allocated by the CPU on demand. When programming, we don't have to control it directly. And there's no way to find out in the program exactly when the register was used. 2. Stack. It is located in random access memory (RAM) and operates through stack pointers (stacks pointer) that are directly supported by the CPU. The stack pointer allocates new storage space by moving down. and frees up storage space by moving up. This is another way to allocate storage space in a high speed and efficient manner after a register. During the creation of the program, The Java compiler must know the exact size of all the data stored on the stack and their lifecycle. This allows it to generate code to move the stack pointer up and down. But this constraint limits the flexibility of the code, so when certain Java stores are on the stack-especially, object references Reference)--and the Java object itself is not stored on the stack. 3. Heap (heap). This is the memory pool (also in the Ram area) where all the Java objects are stored. The advantage of the heap is that it is not like a stack, and the compiler does not need to know how much storage space it should allocate from the heap, or how long the storage space should exist on the heap. How much storage space is allocated on the heap has great flexibility. Whenever you need to create an object, you simply need to use the keyword new in your code to create it. Allocate the appropriate storage space on the heap (heap) when the code is executed. Of course, There is also a price to pay for this flexibility: allocating storage on the heap is more time-consuming than allocating storage on the stack (if you can create objects in Java like C + + on a stack). 4. Static storage area (storage). Static "is represented here as" in a fixed position "(although it is also in RAM). The static storage area includes: Data that is valid for the entire run of the program. You can use the keyword static to specify that a particular object is static. But the Java object itself is never placed in a static store. 5. A constant store (constant storage). Constants are usually placed directly in the program code. It's safe because they neverbe changed. At some point, multiple constants can be separated by themselves, so that they can be arbitrarily placed in a read-only memory (ROM) in an embedded system. 6. Non-RAM storage (Non-ram storage). If the data exists entirely outside of the program, the data can exist without the program being run and not controlled by the program. For such cases, there are two main examples to illustrate: First, object flow (streamed objects), This object is usually sent to other machines in the form of a byte stream. Two, persistent objects (persistent objects), which are stored on disk and remain in their original state even after the program is aborted. These memories are stored in some form in other media, Then revert it to a regular ram-based object when needed. Java provides support for lightweight persistence. In future Java versions, Java will provide more complete support for persistence.
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