Reference from Java programming thought four----2.2.1
When the program runs, it's best to know where the data is stored. A special note is the allocation of memory. There are six places available
To save the data:
(1) Register. This is the fastest save area because it is located in a different place than all other ways of saving: inside the processor. However, the deposit
The number of the converters is very limited, so the registers are assigned by the compiler as needed. We have no direct control over this, and it is impossible to
Any traces of the register are found in the program.
(2) stack. resides in the general RAM (random access memory) area, but can be directly supported by its "stack pointer" for processing. Heap
If the stack pointer moves down, the new memory is created, and if you move up, the memory is freed. This is a particularly fast, particularly effective data-saving
mode, second only to registers. When you create a program, the Java compiler must know exactly the "length" of all the data stored in the stack and the "memory
In time. " This is because it must generate the appropriate code to move the pointer up and down. This restriction undoubtedly affects the flexibility of the program
, so although some Java data is stored in the stack-especially the object handle-the Java object is not placed in it.
(3) heap. A general-purpose memory pool (also in the Ram area) in which Java objects are saved. Unlike stacks, the "memory heap" or
The most interesting part of the heap is that the compiler does not have to know how much storage to allocate from the heap, and it does not have to know what data is stored
How long it takes to stay in the heap. As a result, there is greater flexibility in storing data with heaps. When you want to create an object, you only need to use new
Make the code relevant to the compilation. When the code is executed, the data is saved automatically in the heap. Of course, to achieve this flexibility, it is inevitable
Will pay a price: it takes longer to allocate storage space in the heap!
(4) Static storage. "Static" here means "in a fixed position" (albeit also in RAM). During the operation of the program, the static
The stored data is ready to be called. The static keyword can be used to indicate that a particular element of an object is static. But the Java object itself never
It is far from being placed in a static storage space.
(5) Constant storage. The constant value is usually placed directly inside the program code. It is safe to do so, because they will never change. Some constants
Strict protection is required, so consider placing them in read-only memory (ROM).
(6) Non-RAM storage. If the data is completely independent of a program, the program can still exist when it is not running and outside the control of the program.
The two most important examples are "streaming objects" and "fixed objects". For streaming objects, the object becomes a byte stream, which is usually sent to
Another machine. For fixed objects, objects are saved on disk. Even if the program is aborted, they can still keep their state intact. Right
A particularly useful technique for these types of data storage is that they can exist in other media. They can even be restored once they are needed.
into a common, Ram-based object. Java 1.1 provides support for the lightweight persistence. Future versions may even mention
For a more complete solution.
Where data is stored in Java