Where objects are stored (Java programming Ideas)

Source: Internet
Author: User

Manipulate the object with a reference.
A reference is created that needs to be initialized (associated with a thing) for normal use.
New associates a reference to an object

Where are the objects stored?
When the program is running, how is the object placed in the arrangement? In particular, how is memory allocated? Understanding these aspects can be of great help to you.
There are five different places to store your data:
1, register. This is the fastest storage area, because it is located in a different location than the other storage ———— the inside of the processor. However, the number of registers is extremely limited, so the registers are allocated according to the requirements. You can't directly control or feel any sign of the register in the program.
2, Stack. is located in Universal RAM (random access memory), but can be directly supported from the processor through a 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 system must know the exact life cycle of all items stored in the stack, so that you can move the stack pointer up or 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 common memory pool (also located in the Ram area) for storing all Java objects. The benefit 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. So there's a lot of flexibility in allocating storage in the heap. When you need an object, you simply write a simple line of code with new, and when you execute this line of code, the storage allocation is automatically made in the heap. Of course, there must be a cost to this flexibility: storage allocation and cleanup with heaps can take more time than storage allocation with stacks (if Java can actually create objects in the stack)
4, constant storage. Constant values are usually stored directly in the program's internal code, so it's safe to do so because they never change. Yes, 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). (An example of this store is a string pool, where all literal constant strings and constant expressions with string values are automatically memory-qualified and placed in a special static store)
5, 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. Two basic examples are stream objects and persisted objects. The trick is to convert objects into transactions that can be stored on other media and revert to regular, ram-based objects 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 a database.

Exceptions: Basic data types
A series of types are often used in programming, and they require special treatment. They can be imagined as "basic data Types". The special treatment is because new stores objects in the heap, so new creates an object ———— especially small, simple objects that are often not very effective. So for these types, Java takes the same approach as C and C + +. That is, instead of creating an object with new, you create an "automatic" variable that is not a reference. This variable stores "values" directly and is placed on the stack, making it more efficient.

Where objects are stored (Java programming Ideas)

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.