When running the program, we 'd better know where the data is stored. Pay special attention to the memory allocation. Data can be stored in six locations: the Registers register is the fastest place to store, because all the other storage media of the register are different: it is inside the processor. However, the number of registers is very limited, so the registers are allocated by the compiler as needed.
When running the program, we 'd better know where the data is stored. Pay special attention to the memory allocation. Data can be stored in six places:
Registers register
This is the fastest place to store, because all the other storage media in the register are different: it is inside the processor. However, the number of registers is very limited, so the registers are allocated by the compiler as needed. As a programmer, we have no direct control over this and we cannot feel any signs of register in the program.
Stack
It is located in normal RAM (random-access memory, random access memory. The processor uses its pointer ("stack pointer", stack pointer) to directly support processing. If the stack pointer is moved down (after), a new memory will be allocated. if it is moved up (before), the memory will be released. This is a fast and efficient data storage method, second only to registers. The Java compiler has the responsibility to generate the program code "moving the stack pointer before and after, therefore, it must be able to fully master the actual size and survival time of all data in the stack in the compiled program ". In this way, the flexibility of the program is limited. Due to this limitation, although some Java data is stored in the stack-especially the object handle, Java objects are not put in it.
Heap
Heap is a general-purpose memory storage space (also exists in RAM) used to store all Java objects. "Memory Heap" or "Heap" is better than stack because the compiler does not need to know how much storage space is allocated from the Heap, you do not need to know how long the space allocated from the stack will survive. Therefore, it is more flexible to store data in a heap. You only need to use new to create an object. When the code is executed, space is allocated in the heap. Of course, to achieve this flexibility, we will inevitably pay a certain price: it takes longer to allocate storage space in the heap than to allocate it from the stack (assuming you can actually generate objects from the stack like C ++ in Java )!
Static Storage space
Here "Static" refers to "in a fixed position" (also in RAM ). The static bucket stores data that persists during the running of the program. The static keyword can be used to set a specific member in an object to static, but the Java object itself will never be placed in static storage space.
Constant Storage space
Constant values are usually directly placed in program code. Because they will never change, they are also safe. Some constants must be strictly protected, so you can consider placing them in read-only memory (ROM.
Non-RAM Storage Non-RAM bucket
If the data exists completely outside the program, the data continues to exist when the program is not run, out of the control scope of the program. Two major examples are streamed objects and persistent objects )". In the form of streaming objects, objects are converted into a series of bytes streams, which are usually transmitted to another machine. In the form of persistent objects, objects are stored on disks. even if the program ends, these objects can be retained. This type of storage space is characterized by the ability to convert objects into a form that can be stored in other media, and when necessary, restores the stored data to a common object that can be stored in RAM. Java provides support for "Lightweight persistence. The new version may provide better solutions.
This article is available at http://www.nowamagic.net/librarys/veda/detail/765.