Java stack, heap, method area

Source: Internet
Author: User

1. Stacks and heaps (heap) in Java are places where Java stores data in memory (RAM)

2. Heap Area

All of the objects are stored, and each object contains a class-corresponding information. (The purpose of class is to obtain the operation instructions);

The JVM has only one heap, which is shared by all threads, does not hold base types and object references, and only holds the object itself.

The advantages and disadvantages of the heap: the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, the Java garbage collector will automatically charge these are not used in the number

However, the disadvantage is that the access speed is slow due to the dynamic allocation of memory at run time.

3. Stack area

Each thread contains a stack area that holds only objects of the base data type and references to custom objects (not objects), and objects are stored in the shared heap;

The data in each stack (basic data types and object references) are private and other stacks are inaccessible;

The stack is divided into 3 parts: the basic type variable area, the execution environment context, the operation instruction area (holds the operation instruction)

Advantages and disadvantages of the stack: the access speed is faster than the heap, second only to the register directly in the CPU, but it must be determined that the existence of the data size in the stack and the lifetime must be determined

and lack of flexibility. Data for a single stack can be shared.

Stack: is an advanced post-out data structure that usually holds parameters in the method, local variables.

In Java, all base types and reference types are stored in the stack, and the data in the stack is typically within the current scopes

4. Method area

1, also called the static zone, as the heap, is shared by all threads. The method area contains all class and static variables;

2. The method area contains all the unique elements that are always in the program.

5. In Java, there are six different places where data can be stored:
1. Register (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 and the

The registers are allocated by the compiler on demand. You cannot directly control or feel any sign of register presence in the program.


2. Stacks (Stack). is located in Universal RAM, but it can be supported from the processor with its "stack pointer". If the stack pointer moves down, the new memory is allocated;

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 compiler must know the storage

The exact size and life cycle of all data within the stack, because it must generate the appropriate code 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 do not store them.  


3. Heap. A universal memory pool (also available in RAM) for storing so Java objects. The benefit of a heap different from a stack is that the compiler does not need to know

How many storage areas to allocate from the heap, and how long the stored data will survive in the heap. Therefore, there is great flexibility in allocating storage in the heap. When you need

When you create an object, you only need new to write a simple line of code, and when you execute this line of code, the storage allocation is automatically made in the heap. Of course, for this flexibility it is necessary

Need to pay the corresponding code. Storage allocation with heaps takes more time than storage storage with the stack.  


4. Static storage (storage). "Static" here means "in a fixed position". The data stored in the static store that persists while the program is running. You can use the keyword

Static to identify a particular element of an object is static, but the Java object itself never resides in a static storage space.  


5. Constant storage (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

In an embedded system, the constants themselves are separated from the rest, so in this case, you can choose to place them in ROM


6. 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.
As far as speed is concerned, there are the following relationships:
Registers < Stacks < Heaps < other

Run class procedure: Method area Find Method--instantiation object in heap--call stack (point to instance in heap)

Java stack, heap, method area

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.