In Java, the storage is divided into 6 classes. Register (register), stack (stack), heap (heap), static storage (storage), constant storage (constant storage), and non-random access Storage (Non-ram), respectively.
1. Register (Register). Unlike other stores, it is located in the CPU, so it is the fastest-accessing store. But because the number of registers in the CPU is very limited, the use of registers is allocated by the CPU on demand . There is no need to direct control of it when programming. And in the program there is no way to find out exactly when to use the register of the basis.
2. Stack (stack). It is located in random access memory (RAM) and operates via stack pointers (stack pointer) that are directly supported by the CPU. The stack pointer moves down to allocate new storage space. and to free up storage space by moving up. This is another way to allocate storage space at high speed and efficiently after the 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 life cycle. 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 some Java storage is on the stack-specifically, the object reference Reference)-While the Java object itself is not stored on the stack.
3. Heap. This is the memory pool (also in the Ram area) where all 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 it should allocate from the heap, or how long the storage space should be on the heap. Therefore, there is great flexibility in allocating much storage space on the heap. Whenever you need to create an object, you Just simply use the keyword new in your code to create it. When this code is executed, it allocates the appropriate storage space on the heap. Of course, there is a price to be paid for this flexibility: allocating storage on the heap is more time-consuming than allocating storage space on the stack ( If you can create objects on a stack in Java like in C + +.
4. Static storage area (static storage). " Static "Here means" in a fixed position "(although it is also in RAM). The static storage area includes: Data that is valid for the entire duration of the program. You can use the keyword static to specify that special objects are static. However, the Java object itself is never placed in a static store.
5. Constant storage (constant storage). Constants are usually placed directly in the program code. This is safe, because they can never be changed. At some point, multiple constants can be separated by themselves, so that the embedded system may be arbitrarily placed in the read-only memory (ROM) the.
6. Non-RAM storage (Non-ram storage). If the data exists entirely outside the program, the data can exist without the program's control when the program is not running. For such cases, there are two main examples to illustrate: one, object flow (streamed objects), This object is usually sent to other machines in the form of a byte stream. Two, persistent object (persistent objects), which is stored on disk, They remain in their original state even after the program has been aborted. These memories are stored in some form on other media, and then revert back to regular ram-based objects when needed. Java provides support for lightweight persistence. In future versions of Java, Java will provide more complete support for persistence
Java Store details