memory allocation when the [Java] program runs

Source: Internet
Author: User

    • Source: "Thinking in JAVA"
  1. 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 according to the requirements. You cannot directly control or feel any sign of register presence in the program (on the other hand, C and C + + allow you to suggest how registers are allocated to the compiler).

  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 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 to move the stack pointer up or down. This constraint limits the flexibility of the program, so while 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. Therefore, there is great 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 allocations on the stack (if you can actually create objects in Java as in C + +).

  4. 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 store them in ROM (read-only memory).
    An example of this storage area is the string pool. 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 of the basic examples are stream objects and persisted objects. In a Stream object, the object is converted into a byte stream, which is usually sent to another machine. In persisted objects, objects are stored on disk, so they can remain in their own state even if the program terminates. The trick is to turn objects into things that can be stored in other mediums, 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.

?

[Java] program run-time memory allocation

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.