Storage of Java Data

Source: Internet
Author: User

In Java, there are six different places where data can be stored:

1. Register (Register). This is the fastest storage area because it is located in a different location from the other store-the inside of the processor. However, the number of registers is extremely limited, so the registers are allocated by the compiler on demand. You cannot directly control or feel any sign of register presence in the program.

2. stacks (Stack). is located in Universal RAM, but it can be supported from the processor with its "stack pointer". If the stack pointer moves down, the new memory is allocated, and if you move up, the memory is freed. This is a fast and efficient method of allocating storage, second only to registers. When you create a program, the Java compiler must know the exact size and lifecycle of all the data stored in the stack, because it must generate the appropriate code to move the stack pointer up and down. This constraint limits the flexibility of the program, so while some Java data is stored on the stack-especially object references-the Java object does not store it.

3. Heap. a universal memory pool (also available in RAM) for storing so Java objects. The benefit of a heap that differs from the stack is that the compiler does not need to know how many storage areas to allocate from the heap, or how long the stored data survives in the heap. Therefore, there is great flexibility in allocating storage in the heap. When you need to create an object, you only need new to write a simple line of code, and when you execute this line of code, it will be automatically stored in the heap allocation. Of course, for this flexibility you have to pay the code accordingly. Storage allocation with heaps takes more time than storage storage with the stack.

4. static storage (storage). "Static" here means "in a fixed position". The data stored in the static store that persists while the program is running. You can use the keyword static to identify a particular element of an object that is static, but the Java object itself never resides in a static storage space.

5. constant Storage (constant storage). constant values are usually stored directly inside the program code, and it is safe to do so because they are never changed. Sometimes, in an embedded system, the constants themselves are separated from the rest, so in this case, you can choose to place them in ROM

6. non-RAM storage. If the data is completely outside of the program, it can be free from any control of the program and can exist when the program is not running.

As far as speed is concerned, there are the following relationships:

Registers > Stacks > Heaps > Other heaps and stacks:

Heap: Heaps are heap, which is called dynamic memory, where there is no need to reclaim when it is needed to allocate to new memory requests, and its in-memory data is unordered , i.e. the allocated and subsequently allocated memory does not have any necessary positional relationships, They can also be released without sequencing. It is generally freely allocated by the user , and malloc allocates the heap, which needs to be released manually.

Stack: is stack. In fact, there is only one exit queue, that is, last in, first Out (firstinlastout), the first allocated memory must be released. Generally, by the system automatically allocated, storing the parameters value of the function, local variables, etc., automatically cleared.

Also, the heap is global, and the stack is a small chunk when each function enters , and the function is released when it returns.

static and global variables, new variables, all placed in the heap Local variables are placed on the stack , so the function returns, and the local variables are all gone.

In fact, in practice, stacks are used to store calls to methods. The heap is used for object storage.

The basic types in Java, in fact, need special treatment. Because, in Java, objects created with new are stored in the heap, it is often not very effective to create a small, simple variable, such as a basic type, with new. Therefore, in Java, for these types, the same methods as C and C + + are used. That is, instead of creating a new, you create an "automatic" variable that is not a "reference". This variable has its "value" and is placed on the stack, making it more efficient.

Heaps and stacks in the JVM

The JVM is a stack-based virtual machine. The JVM allocates a stack for each newly created thread . In other words, for a Java program, it runs through the operation of the stack. The stack holds the state of the thread in frames.

The JVM operates on the stack in only two ways: stack and stack operations in frames.

We know that the method that a thread is executing is called the current method of this thread. We may not know that the frame used by the current method is called the current frame. When a thread activates a Java method, the JVM presses a new frame into the Java stack of threads. This frame naturally becomes the current frame. During the execution of this method, this frame is used to hold parameters, local variables, intermediate calculation procedures, and other data. This frame is similar to the concept of the activity record in the compilation principle.

From this allocation mechanism in Java, the stack can be understood as a stack is a storage area that the operating system establishes for a process, or a thread (a thread in a multithreaded operating system) for this thread, which has an advanced post-out feature.

Each Java application uniquely corresponds to a single JVM instance, and each instance uniquely corresponds to one heap. All instances or arrays of classes created by the application in the run are placed in this heap and shared by all threads of the application. Unlike C + +, allocating heap memory in Java is automatically initialized. The storage space for all objects in Java is allocated in the heap, but the reference to this object is allocated on the stack, that is, allocating memory from two places when an object is built, memory allocated in the heap actually establishes the object, and the memory allocated on the stack is just a pointer to the heap object (reference) Only.

In particular, say:

Stacks and heaps 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.

A heap is a run-time data area from which the object of a class allocates space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time. objects and arrays in Java are stored in the heap.

The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared . However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object references.

Stack has a very important particularity, is that there is data in the stack can be shared. Let's say we define both:

int a = 3;

int b = 3;

The compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it stores the 3 in and then points a to 3. then the int b = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, B points directly to 3. In this case, A and B both point to 3. At this point, if you make a=4 again, then the compiler will re-search the stack for 4 values, if not, then store 4 in, and a point to 4; Therefore the change of a value does not affect the value of B. It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of a does not affect B, which is done by the compiler, which facilitates space saving. An object reference variable modifies the internal state of the object, affecting another object reference variable.

Where is the Java element and where the store belongs

When developing applications on the Java platform, there is a big feature of creating objects when the application is running. In other words, when the program is running, it will ultimately determine the attribution of the object, that is, where the object should be stored. Because they are stored in different regions, they vary in performance. To do this, developers of Java programs need to understand the characteristics of each storage area and the impact on performance. Then adjust the application's regional allocations as needed. In general, there are five places in the operating system that you can use to save data in your application's running. The characteristics of these areas and their impact on performance are analysed below.

Save area One: register.

Although they are in memory, different areas have different performance depending on their purpose. In the case of a Java application, the register is the fastest to access data for this area because it is inside the processor. is vastly different from other storage areas in memory. So do we put all the objects in this area and not improve the performance of the Java application? In theory, but not in reality. Because the number of registers is very limited . The register area in memory is allocated by the compiler as needed. Our program developers are not able to control the allocation of this register through code. So, this first storage area register, we can only look at, and not be able to have any effect on it.

Save area Two: stack.

There are two ways to create objects, one is to create objects during application development, and the other is to create objects when they are used in the process of running the program. The former has higher performance than the latter, while the latter is more flexible than the former. This is mainly because the former creates the object, which is created in this stack. Although the object it creates is not saved in the register, the push-stack pointer through this object can be used directly from the processor where the relevant support is obtained. If the stack pointer moves upwards, the memory occupied by the original object is freed, and the object is allocated new memory if the stack pointer moves down. So, if the object is stored in this stack, performance is not as ideal as storing it in a register, but it is still much better than storing it elsewhere.

because Java programs create objects as needed during the program's run. This object cannot be saved in this stack. But Java applications cannot waste this precious space in vain. For this reason, although the Java object itself is not stored in this stack (not without saving it, but there is no place for him), it is still better to put some of the content that can be put into this stack to improve the performance of the application. If you can store some object references in this stack.

In addition, for some basic data type objects, Java programs tend to put them on the stack to improve the performance of data processing. such as some integer, character data objects, these objects have some common characteristics, such as small objects, is the Java program provides standard objects and so on. For these objects because each application is basically needed, and our program developers can only reference these objects, and not be able to change them. For this purpose, Java programs tend to create objects at the outset (that is, creating objects directly on the stack and saving them), rather than just creating them when needed, as with other objects. There is an important reason to create these objects in the stack only. Because if an object is created on the stack, the Java editor must know the exact size and lifecycle of all data stored in the stack. In order to obtain this information, the relevant code must be generated to obtain this information so that it can manipulate the stack pointer. Normal object size, life cycle, and so on are difficult to obtain beforehand, to create a normal object in the stack, is not very suitable for Java applications. Conversely, the predefined object sizes of these Java compilers do not change as the machine hardware architecture changes and user requirements change, and these objects tend to exist from the beginning, so there is no life cycle problem. So it is reasonable and achievable to put these objects on the stack. This processing not only does not affect the flexibility of the object, but also provides better performance.

Save area Three: heap.

Heaps, like stacks, are random accesses to areas in memory, but they differ greatly. Because there is no stack pointer in the heap , there is no way to get support directly from the processor. For this reason, compared with the stack performance, there is a certain gap. In general, except for some of the predefined objects mentioned above, other objects are stored in this heap . In other words, objects created with the New keyword are saved in the heap . The benefits of saving in the heap are also obvious. As the Java compiler does not need to know how many storage areas to allocate from the heap, it is not necessary to know how long the stored data will survive in the heap. So allocating storage in the heap is a lot of flexibility. When an object is needed, we can use the New keyword to create an object. The system will then automatically assign an area in the heap to the object as a destination. The biggest disadvantage, however, is that it is much slower to create objects and allocate storage areas in the heap than in the stack. You can't have your cake and eat it.

Storage Area Four:static storage area and constant storage area。

There are some special elements in the Java object. For example, some elements are more specific (such as variables defined with the keyword static). These variables may be static for other objects. To better manage these variables, Java specifically divides a static storage area in memory to manage these elements. The static storage area here refers to the data that persists while the application is running in a fixed location . One thing to be clear about here is that Java objects are not stored in this place, but only some of the special elements in the object are placed here. Because the location is fixed, so the next call to eliminate the trouble of finding. This is advantageous for providing the performance of the application. As a developer of our program, when writing code, we need to apply the static keyword flexibly. The author's opinion is that you can use it, and you can't use it when you want to use it. In particular, some elements with the use of the Static keyword for the program function has no effect, at this time we have to confidently add the static keyword in front of the element.

There is a special kind of element in Java objects, which we call constants. Because the constant value is stable, such as pi. It is possible to put them inside the code for this purpose. Sometimes, however, we do not do this when we are doing some embedded system development. Instead, the constant elements are kept separate from the code. If we keep the value of the constant in some read-only memory according to the situation . This is mainly for a number of special functions to consider. such as the need for copyright control. such as on the printer in order to protect the copyright of original supplies, often the constant with the code stored separately.

Storage Area five: non-RAM storage.

Sometimes, some programs run the data that we need to place in other places. If a stream object is needed in some systems, the object's data is not stored in any of the storage areas mentioned above, and the object is directly converted to a byte stream and sent to the other host. There is also an object called persistence, which is stored on the hard disk. These objects are not often used in the application development process, you just need to understand the existence of these objects. It's not too late to delve into it until it needs to be used.

From the above analysis we can see that the attribution of the object to our program developers is difficult to control. Registers are managed by the compiler. Heap and Stack are basically limited by the development platform, and our program personnel have no such ability to interfere with them. In fact, we are mainly able to adjust and control is the fourth storage area, that is, static storage and constant storage. The author's suggestion is that, for non-embedded programs, can use static storage to achieve, as far as possible to use static storage. For constants, it is necessary to determine whether the constants need to be stored in read-only memory, depending on the functionality needed to implement them. Sometimes the protection of copyright and so on need to use this read-only memory.

Source: https://www.2cto.com/kf/201612/572697.html

Storage of Java Data

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.