This article is excerpted from Java programming thoughts
When running the program, we 'd better know where the data is saved. Pay special attention to the memory allocation. Data can be stored in six places:
(1)Register. This is the fastest storage region, because it is located in a different place from all other storage methods: Inside the processor. However, the number of registers is very limited, so the registers are allocated by the compiler as needed. We have no direct control over this, and it is impossible to find any trace of the Register in our program.
(2)Stack. It resides in the regular RAM (Random Access to memory) region, but can be directly supported by its "Stack pointer. If the stack pointer moves downward, a new memory is created. If it moves upward, the memory is released. This is a fast and efficient data storage method, second only to registers. When creating a program, the Java compiler must accurately know the "length" and "time of existence" of all data stored in the stack ". This is because it must generate the corresponding code to move the pointer up and down. This restriction undoubtedly affects the flexibility of the program. Although some java data must be stored in the stack, especially the object handle, Java objects are not put in it.
(3)Heap. A general-purpose Memory Pool (also in the ram region) that stores Java objects. Unlike stack, "memory Heap" or
The most attractive aspect of heap is that the compiler does not have to know how much storage space is allocated from the heap or how long the stored data will stay in the heap. Therefore, it is more flexible to store data in a heap. When creating an object, you only need to use the new command to compile the relevant code. When the code is executed, data is automatically saved in the heap. Of course, to achieve this flexibility, you must pay a certain price: it takes longer to allocate storage space in the heap.
(4)Static storage. Here "static" refers to "in a fixed position" (although also in Ram ). During the running of the program, the static storage data will be waiting for calling at any time. The static keyword can be used to indicate that a specific element of an object is static. However, Java objects are never placed in static buckets.
(5)Constant Storage. Constant values are usually placed directly inside the program code. This is safe because they will never change. Some constants must be strictly protected, so you may consider placing them in read-only memory (ROM ).
(6)Non-ram Storage. If the data is completely independent from a program, the program may still exist when it is not run and is out of the control scope of the program. Two major examples are "stream object" and "fixed object ". For stream objects, the object is converted into a byte stream, which is usually sent to another machine. For fixed objects, objects are stored on disks. Even if the program stops running, they can remain in their own State. A particularly useful technique for these types of data storage is that they can be stored in other media. Once needed, they can even be restored to common, Ram-based objects. Java 1.1 supports lightweight persistence. Future versions may even provide more complete solutions.