Java memory management

xiaoxiao2021-03-06  47

- The following translated from "Thinking in Java"

When the program is running, we'd better save the data to where you have a number. It is important to pay attention to the distribution of memory. There are six places.

Save data:

(1) Register. This is the fastest storage area because it is located in different places in all other saving methods: the internal processor. However, storage

The quantity of the unit is very limited, so the register is assigned by the compiler as needed. We have no direct control, and we can't be in your own.

Find any traces that the registers exist in the program.

(2) Stack. Reside on regular RAM (Random Access Memory) area, but can be processed directly through its "stack pointer". heap

If the stack pointer moves down, a new memory will be created; if you move, those memory will be released. This is a particularly fast, special and effective data preservation

Way, second only to the register. When you create a program, the Java compiler must accurately know the "length" of all the data saved in the stack and "save

At time. "This is because it must generate the corresponding code to move the pointer up and down. This limitation undoubtedly affects the flexibility of the program.

Sex, so although some Java data should be saved in the stack - especially object handles, Java objects do not place it.

47

(3) Heap. A conventional use of memory pool (also in the RAM area), which saves Java objects. Unlike the stack, "memory stack" or

"Heap" (Heap) The most attractive place lies in the compiler that you don't have to know how much storage space is to be allocated from the stack, and you don't have to know the stored data.

How long is you staying in the stack. Therefore, greater flexibility is obtained when saving data with a stack. When you create an object, just use new life.

Let the relevant code to prepare it. When you perform these code, you will automatically save the data in the stack. Of course, in order to achieve this flexibility, it is inevitable.

Will pay a certain price: I will take a longer time when allocating the storage space!

(4) Static storage. "Static" here refers to "located in a fixed position" (although in the RAM). Quiet

The data stored in state will wait any time. The STATIC keyword can be used to indicate a particular element of an object is static. But the Java object itself is forever

Will not be placed in static storage space.

(5) constant storage. The constant value is usually placed inside the program code. This is safe because they will never change. Some constant

Need strictly, it is considered to place them into read-only memory (ROM).

(6) Non-RAM storage. If the data is completely independent of a program, the program can still be present when the program is not running, and outside the program's control range.

Two of the most important examples are "flow objects" and "fixed objects". For streaming objects, the object will become byte stream, usually sent

Another machine. For fixed objects, the object is saved in the disk. They still keep their own status unchanged even if the program is aborted. Correct

In these types of data stores, a particularly useful technique is that they can exist in other media. Once needed, you can even recover them

It is common, based on RAM-based objects. Java 1.1 provides support for LightWeight Persistence. Future version or even possible

For more complete solutions.

转载请注明原文地址:https://www.9cbs.com/read-82386.html

New Post(0)