Storage area for Java objects and elements

Source: Internet
Author: User

One of the great features of developing applications on the Java platform is that they build objects when the application is running. In other words, when the program is running, it will ultimately determine where the object belongs, that is, where the object should be stored. Because they are stored in different areas, they vary in performance. As a Java program developer, you need to understand the characteristics of each storage area and its impact on performance. Then adjust the application's zone allocations as needed. In general, there are five places in the operating system that can be used to save data in the running of an application. The characteristics of such zones and their impact on performance are analyzed below.

Storage Area One: registers

Although the same in memory, but different areas because of the use of different, its performance is also different. In the case of a Java application, the register is the fastest access to data because it is inside the processor. is vastly different from other storage areas in memory. So if we put all the objects in this area, can we improve the performance of the Java application? Theoretically, but not in reality. Because the number of this register is very limited. The register area in memory is allocated by the compiler on demand. 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 it without being able to have any effect on it.

Storage Area Two: stacks

There are two ways to create an object, one is to create the object during the development of the application, and the other is to create the object when it is used in the process of running the program. The former is higher than the latter, and 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 created is not stored in the register, the push-through pointer to this object can be used directly from the processor to obtain relevant support. When the stack pointer moves up, releases the memory occupied by the original object, and allocates new memory for the object, such as when the stack pointer moves down. Therefore, if the object is stored in this stack, although the performance is not as good as stored in the register, but still more than stored in the other place much better.

Because Java programs are created when the program is running, you create objects based on your needs. This object cannot be saved on this stack. However, Java applications can not waste this valuable space in vain. For this reason, although the Java object itself is not stored on this stack (rather than being saved but there is no room for it here), it should be possible to put something that can be put into this stack to improve the performance of the application. If you can put some object references on this stack.

In addition, for some basic data type objects, Java programs tend to place them on the stack to improve the performance of data processing. such as some integer, character-type data objects, these objects have some common characteristics, such as the object is relatively small, is the Java program to provide the standard object and so on. These objects are basically needed for each application, and our program developers can only reference them and not make changes to them. For this Java program, when it is processed, it often starts by creating objects (that is, creating objects and saving them directly on the stack), rather than creating them when needed, like other objects. There is also an important reason to create these objects in the stack. Because if you create an object on the stack, the Java editor must know the exact size and lifecycle of all the data stored in the stack. In order to get this information, the relevant code must be generated to obtain this information so that it can manipulate the stack pointer. Normal object size, lifecycle, and so on are difficult to obtain beforehand, so creating normal objects on the stack is not appropriate for Java applications. Instead, the predefined object sizes of these Java compilers do not change as the machine hardware architecture changes and user requirements change, and these objects often exist from the beginning, so there is no lifecycle problem. So it is reasonable and achievable to place these objects on the stack. This approach not only does not affect the flexibility of the object, but also provides better performance.

Storage Area Three: heap

The heap, like the stack, is an area of random access to memory, but the two are very different. Because there is no stack pointer in the heap, there is no way to get support directly from the processor. For this reason its performance compared with the stack, there is a certain gap. Normally, except for some of the predefined objects mentioned above, other objects are stored in this heap. Alternatively, objects created with the new keyword are stored in the heap. The benefits of being kept in the heap are also obvious. such as the Java compiler does not need to know how many storage areas to allocate from the heap, nor does it have to know how long the stored data will survive in the heap. So there's a lot of flexibility in allocating storage in the heap. When an object is needed, we can use the New keyword to create an object. The system then automatically assigns the object to the heap to assign a zone to its destination. The biggest disadvantage, however, is that it is much slower to create objects and allocate storage areas in the heap than on the stack. You can't have your cake and eat it.

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.