Java object storage in Java programming ideology

Source: Internet
Author: User

The following content is taken from the third edition of Java programming ideology.:

How do I arrange objects when the program is running? In particular, how is the memory allocated? There are six different places to store data:

1. registers:This isFastestBecause it is located in a different place from other storage areas --Inside the processor. However, the number of registers is extremely limited, so registers are allocated as needed. You can neither directly control the register, nor show signs of existence in the program.

2. Stack: Located in generalRam (access storage immediately)But pass"Stack pointer"You can obtain direct support from the processor. If the stack pointer moves down, a new memory is allocated. If the stack pointer moves up, the memory is released. This is a fast and effective way to allocate storage,Only times and registers.Java SystemYou must know the exact lifecycle of all items stored in the stack.To move the stack pointer up or down. This constraint limits the flexibility of the program, although some java data is stored in the stack --- especiallyObject Reference,However, Java objects are not stored in them.

3. Heap:A general memory pool (also located inRamArea), usedStore all Java objectsStack has the following advantages:The compiler does not need to know how many storage areas are allocated from the heap, nor how long all the data stored in the heap will survive in the heap.. Therefore, it is flexible to allocate storage in the heap. But when you need an object, you only needNewWrite a simple line of code. When this line of code is executed, it is automatically stored and allocated in the heap. Of course, there must be a price for this flexibility: heap-based storage allocation and cleanup may be better than stack-based storage allocation.More Time Required.

4. static storage:Here "static" refers to "in a fixed position" (althoughRam). Static storage stores the data that persists when the program is running. Available keywordsStaticTo identify a specific element of an object is static.However, Java objects are never placed in static buckets.

5. Constant storage:Constant values are usually stored directly inInternal program codeThis is safe because they will never be changed. Sometimes, in an embedded system, constants are isolated from other parts, so in this case, you can choose to save themRom(Read-only memory.

6. Non-ram storage:If the dataCompletely survive the programSo it can not be controlled by the program, it can also exist when the program is not running, two basic examples are:Stream object and persistence object. In a stream object, the object is converted into a word throttling, which is usually sent to another machine. In persistent objects, objects are stored on disks, so they can remain in their own State even if the program is terminated. The trick of this storage method is to convert objects into things that can be stored on other media and restore them to conventional, Ram-based objects as needed. Java provides support for lightweight persistent objects.

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.