How Java objects are stored

Source: Internet
Author: User

1. Registers. This is the fastest storage area, because it is stored directly inside the processor, due to the limited number of registers, registers will be allocated according to needs, in the program we can not directly control, also can not feel the existence of any signs of registers; C and C + + allow the register to be recommended to the registers, however.

2. Stack. is located in Universal RAM (random access memory), but can receive direct support from the processor through the stack pointer. If the stack pointer moves downward, the new memory is allocated, and the memory is freed if you move up. This is a fast and efficient method of allocating storage, second only to registers. When you create a program, the Java system must know the exact lifecycle of all items stored in the stack to move the stack pointer up and down. This constraint limits the flexibility of the program, so although some Java data is stored on the stack-especially object references-Java objects are not stored in them.

3. Heap. A general-purpose memory pool (also located in RAM) for storing all Java objects. The advantage of a heap that differs from the stack is that the compiler does not need to know how long the stored data will survive in the heap. Therefore, allocating storage in the heap has great flexibility. When an object is needed, only a new line of code is required, and when this line of code is executed, storage allocations are automatically made in the heap. Of course, there is a price to pay for this flexibility: storage allocation and cleanup with heaps may take more time than storage allocations with stacks (if you do create objects in the stack in Java, as in C + +).

4. Constant storage. Constant values are usually stored directly inside the program code, which is safe, so they will never be changed. Sometimes, in an embedded system, the constants themselves are separated from the rest, so in this case, you can choose to store them in ROM (read-only memory).

5. Non-RAM memory. If the data is completely alive outside of the program, it can be free of any program control and can exist when the program is not running. The two basic examples are streaming objects and persistent objects. In a Stream object, an object is converted into a byte stream, which is usually sent to a machine. In persistent objects, objects are stored in public security, so they can maintain their state even if the program terminates. The trick is to transform an object into something that can be stored on other media and revert to a regular, ram-based object when needed. Java provides support for lightweight persistence, and mechanisms such as JDBC and hibernate provide more complex support for storing and reading object information in the database.

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.