Java Learning (iii): Java Storage classification

Source: Internet
Author: User

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-to-access storage area. But because the number of registers in the CPU is very limited, So the use of registers is allocated by the CPU on demand. When programming, we don't have to control it directly. And in the program there is no way to find out exactly when the register is used.

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 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, unlike stacks. Therefore, It is very flexible to allocate much storage space on the heap. Whenever you need to create an object, you simply need to create it using the keyword new in your code. 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 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 code of the program. This is safe, because they can never be changed. At some point, multiple constants can be separated by themselves, so that in an embedded system it is arbitrary to be placed in read-only memory (ROM).

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, persisted objects (persistent objects), which are stored on disk, even after the program has been aborted, they remain in their original state. These memories are stored in some form of the object in other media, Then restore it to a regular ram-based object when needed. Java provides support for lightweight persistence. In the future Java release, Java will provide more complete support for persistence.

The difference between stacks and heaps

1. Stacks and heaps (heap) are places that Java uses to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.
2. The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. In addition, the stack data can be shared. The advantage of the heap is that the memory size can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand that the Java garbage collector automatically collects the data that is no longer in use. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.

3. There are two types of data in Java. One is the basic type (primitive types), a total of 8 kinds, namely int, short, long, byte, float, double, Boolean,char. The definition of this type is through such as int a = 3; A long b =255l is defined in the form of a, called an automatic variable. It is worth noting that the automatic variable is a literal value, not an instance of a class, that is not a reference to a class, there is no class here. such as int a = 3; Here A is a reference to the int type, which points to the literal value of 3. The data of these literals, due to the size of the known, the lifetime of the known (these values are fixed in a program block, the program block exits, the field value disappears), for the sake of speed, it exists in the stack. In addition, the stack has a very important particularity, is that there is data in the stack can be shared. Suppose we also define
      int a = 3;   int b = 3;
The compiler processes int a = 3 First, it creates a reference to the variable A in the stack, and then looks for an address with a literal value of 3. , open an address that holds the literal value of 3, and then point A to the address of 3. then the int b = 3 is processed, and after the reference variable of B is created, B is pointed directly to the address of 3 because there are already 3 literals in the stack. In this case, A and B both point to 3.

It is particularly important to note that the reference to this literal is different from the reference to the class object. Assuming that a reference to two class objects points to an object at the same time, if an object reference variable modifies the internal state of the object, then another object reference variable will immediately reflect that change. Conversely, modifying its value by a reference to a literal value does not result in another case where a reference to that literal is changed. As in the example above, we define the value of a and B and then make a=4; then B will not be equal to 4 or equal to 3. Inside the compiler, when it encounters A=4, it will re-search the stack for a literal value of 4, and if not, re-open the value of the address 4, and if so, point a directly at the address. Therefore the change of a value does not affect the value of B.
The other is the wrapper class data, such as Integer, String, double, and so on, the corresponding basic data types are wrapped up class. These classes of data all exist in the heap, and Java uses the new () statement to tell the compiler that it is dynamically created as needed at run time, so it is more flexible, but the disadvantage is that it takes more time.

Java Learning (iii): Java Storage classification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.