Heap and stack in the memory allocation policy in Java

Source: Internet
Author: User

According to the compilation affair allocation concept, there are three strategies for memory allocation during runtime: static, stack, and heap.

Static Storage Allocation refers to the ability to determine the storage space requirements of each data policy at runtime during compilation. Therefore, a fixed memory space can be allocated to each data policy during compilation. This allocation policy requires no variable data structures (such as variable arrays) in the Code, or nested or recursive structures, because of their cities, the compilation track cannot care about the actual storage space requirements.

Stack-based storage allocation, also known as dynamic storage allocation, is achieved by a running stack similar to a warehouse. In contrast to static storage allocation, in stack-based storage solutions, the requirements for data areas are completely unknown during compilation and can only be known at runtime, however, when you enter a track module during running, you must know the data zone required by the track module to allocate memory for it. Like the stack we are familiar with in the data structure, stack-based storage allocation follows the principle behind the predecessors.

Static storage allocation requires that the storage requirements of all variables be known during compilation, and stack-based storage allocation requires that all storage requirements be known at the entrance of the process, heap Storage allocation is dedicated to the memory allocation of data structures that cannot be determined at the module import during compilation or runtime, such as variable length strings and object instances. Heap consists of a large number of operable blocks or blocks, heap memory can be allocated and released randomly.

Stack and stack

In programming, for example, C/C ++, all the system misappropriation is carried out through the process stack, all the local variables, the form parameters are the eve of the stack to allocate the memory space. In reality, there is no allocation, but the top of the stack can be used up, just like the conveyor belt in the workshop (Conveyor Belt), stack pointer will automatically guide you to the position of the work, all you have to do is put down the tool. When you exit the function, you can change the stack pointer to destroy the content in the stack. This mode is the fastest, of course, used to run the track. It should be noted that, at the time of allocation, we should know the details of this data zone in advance when allocating data areas for a module that is about to be misappropriated, that is, although the allocation is in the process
It is executed in sequence, but many of the allocated details are fixed and remain unchanged. This "Several details" is determined during compilation, not during runtime.

Heap is the application track fan in the runtime request the operating system to allocate their own memory, because the large operating system hitting the memory allocation, so in the allocation and destruction of the time to occupy the time, the efficiency of using the heap is very low. However, the benefit of heap is that the compiler does not have to know how long it will take to allocate a lot of storage space in the heap or how long the stored data will be stored in the heap, when using the heap to retain data, it will obtain greater flexibility. In fact, the heap memory allocation is essential for the polymorphism of the surface to the object, because the storage space required by the polymorphism variable can be determined only after the object is created at runtime. In C ++, when it is required to create an object, you only need to use the new call to compile the relevant code. When the code is executed, the data is automatically retained in the heap. Of course, in order to achieve this kind of agility, the inevitable price will inevitably be spent: It will be spent when the storage space is allocated in the heap
Longer time of disappearance! This is exactly the reason why we are talking about low efficiency. It seems that comrade Lenin is good at saying that the interests of people are often the mistakes of people, and the mistakes of people are often the interests of people.

Heap and stack in JVM

JVM is a repository-based Virtual Machine. JVM assigns a repository to each thread in the new tree. That is to say, for a Java track, its operation is done through operations on the warehouse. The status of the thread retained by frame in the repository. JVM only performs two types of operations on the Repository: frame-based pressure stack and outbound stack operations.

We know that the method in which a thread is being executed is the current method of this thread. We may not know that the current frame is called the current frame. When a thread activates a Java method, the JVM will press a new frame in the Java repository of the thread. This frame naturally becomes the current frame. In this era of execution, this frame will be used to retain parameters, local variables, central processes and other data. This frame is similar to the practice recorded in the compilation affair.

From the perspective of this distribution mechanism in Java, the warehouse can be understood as follows: the warehouse (stack) is a process or thread when the operating system was established (in a multi-threaded operating system, it is a thread) A storage area established for this thread. This area has the features of its predecessors.

Each Java application corresponds to one JVM instance, and each instance corresponds to one heap. All the class instances or arrays created by the Application track in the running state are put in this heap and shared by all the threads of the application. In disagreement with C/C ++, Java heap memory allocation is automatically initialized. In Java, the storage space of all objects is allocated in heap, but the reference of this object is allocated in the warehouse, that is, when the heap consumer sets up an object, the two locations in the year and night are allocated with memory, and the object is actually set up in heap allocation memory, the memory allocated in the warehouse is just a pointer to the heap object (reference.

Specifically:

Both stack and stack are the places where Java is used to store data in Ram. In disagreement with C ++, Java automatically clicks stacks and stacks, and track Engineers cannot directly set stacks or stacks.

The Java heap is a runtime data zone, and the object of the class is allocated space. These objects are established through commands such as new, newarray, anewarray, and multianewarray. They do not need the rail code to explicitly run Deng. The heap is taken over by the garbage collector. The advantage of the heap is that the memory can be dynamically allocated, so you do not have to tell the compiler in advance during the retention period because it allocates the memory dynamically at runtime, the Java receiver automatically collects the data that is no longer in use. However, the error is that the memory needs to be dynamically allocated at runtime, resulting in slow access. Objects and arrays in Java are stored in the heap.

The advantage of stack is that the access speed is faster than the heap, And the stack data can be shared only by the writer. However, the error is that the data size and retention period in the stack must be determined, and there is a lack of flexibility. The stack first stores some underlying types of variables (, Int, short, long, byte, float, double, Boolean, char) and object references.

Stack has a very important extraordinary feature, that is, the data in the stack can be shared. Let's also say:

Int A = 3;

Int B = 3;

The compiler first blocks int A = 3. First, it will build a reference with a variable as a in the stack, and then find whether there is 3 in the stack. If it is not beautiful, it cannot be found, store 3 and point A to 3. Then, the value of int B = 3 is taken by surprise. After the reference variable of B is created, B is directed to 3 because there is already 3 in the stack. In this case, both A and B point to 3 at the same time. At this time, if it is not beautiful, then make a = 4; then the compiler will search for whether there are 4 values in the stack. If it is not beautiful, it will store 4 in, and point A to 4. If there is no appearance, direct a to this address. Changing the value of A does not affect the value of B. Attention should be paid to the sharing of this type of data and the sharing of the two objects pointing to one object at the same time. This is because in this case, the transformation of a will not affect B,
It is completed by the compiler, which facilitates space saving. A variable referenced by an object is changed to the internal condition of the object, which affects the variable referenced by another object.

PS: Memory Allocation for C ++

The memory occupied by C/C ++ compilation is divided into the following departments:

1. STACK: the compiler automatically stores the parameter values and local variable values of the function. Its operation method is similar to the stack in the data structure.

2. Heap: Generally, the track fan is divided into pan Peng. If the track fan is not released, the OS may take over the track fan event. Paying attention to it is different from the heap in the data structure, but the distribution method is similar to the linked list.

3. Global (static)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The system was released after the track fan was launched.

4. Text Constant Area-constant strings are placed here. The track fan was released by the system afterwards

5. Track code area-stores the binary code of the function body.

Example

This is written by a senior. It is very specific.

// Main. cpp

Int A = 0; global initialization Zone

Char * P1; uninitialized globally

Main ()

{

Int B; stack

Char s [] = "ABC"; stack

Char * P2; stack

Char * P3 = "123456"; 123456 is in the constant zone, and P3 is on the stack.

Static int C = 0; Global (static) initialization Zone

P1 = (char *) malloc (10 );

P2 = (char *) malloc (20 );

The allocated 10-byte and 20-byte areas are in the heap area.

Strcpy (P1, "123456"); 123456 is placed in the constant area, and the compiler may optimize it into a place with the "123456" that P3 points.

}

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.