Compiled in accordance with principles of the memory allocation strategy point of view, the program run-time memory allocation There are three strategies, namely, static, stack-style, and heap-based.
Static storage allocation is determined at compile time will be able to target run time for each data storage space requirements, so at compile time you can give them a fixed allocation of memory space. This allocation strategy requires the program code are not allowed in the variable data structures (such as variable arrays) the existence, but also does not allow any nested or recursive structure is because they will cause the compiler can not calculate the exact storage space requirements.
Stack storage allocation may also be known as the dynamic storage allocation, similar to the stack by a run-time stack to achieve. And static memory allocation of the contrary, in the stack storage program, the program needs the data area is completely unknown at compile time , only to be able to know when to run, but it provides access to a program running in the module, you must know the data required for the program module size will be able to assign memory. and our well-known data structures like stack , stack storage allocated in accordance with the principles of advanced post-out of the distribution.
Static storage allocation required at compile time to know all the variables of the storage requirements, stack storage allocation requests in the process, the entrance must know all of the storage requirements, while the heap storage allocation is responsible for at compile time or run-time module at the entrance to all can not determine the storage requirements of the data structure, memory allocation, such as variable-length strings, and object instances. heap by the availability of large blocks and spare blocks, heap memory can be allocated and released in accordance with any order.
Comparison of the heap and the stack above the definition of principles of teaching materials from the summary compiled from, in addition to static storage allocation both within and outside looked very stiff and difficult to understand, the following aside static storage allocation, focuses on the comparison heap and stack:
From the heap and stack the functions and role in popular comparison is mainly used to store the object heap, stack is mainly used to perform procedures. But again this difference was mainly due to the characteristics of heap and stack the decision:
In the programming, such as C / C + +, all the method calls are carried out through the stack, all the local variables, formal parameters are allocated from the stack memory space. In fact is not a distribution, only from the stack up with the line, just like the factory conveyor belt (conveyor belt), like, Stack Pointer will automatically guide you to put things in position, you have to do is break things down to the line. When out of the function, modify the stack pointer can be to destroy the contents of the stack. Such a mode of the fastest, of course, be used to run the program. be noted that, in the allocation of time, for example, for a program about to call the the allocation of data area module, we should know in advance the size of this data area, also said that although the allocation is carried out in the program is running, but the size distribution of the number is certain, unchanging, and this "small number" is the compile-time determined, not at run time.
The heap is the application program is running when the request for the operating system assigned to its own memory, due to the management of memory allocation from the operating system, so when you have to take up distribution and elimination of time, so the efficiency of using the heap is very low. But the heap has the advantage that the compiler do not have to know how much storage space allocated from the heap, but also having to know the data stored in the heap to stay there a long time, therefore, when you save the data with the heap will be greater flexibility. In fact, object-oriented polymorphism, heap memory allocation is essential, because the multi-state variable storage space required is only created at run time can be determined after the object. In the C + +, require the creation of an object, Just with the new order the preparation of the relevant code. Implementation of these codes, it will in the heap to save the data automatically. Of course, to achieve this flexibility, is bound to pay a certain price: the allocation of storage space in the heap will spend a longer time! This is exactly the result we have just mentioned the reasons for low efficiency, it seems, Comrade Lenin said, well, who are often the advantages of a person's shortcomings, people's shortcomings are often a person's advantages (halo ~).
JVM's heap and stack
JVM is a stack-based virtual machine. JVM for each newly created thread is assigned a stack. In other words, for a Java program, it is through the operation of the operation of the stack to complete. Stack frame as a unit to save the thread state. JVM is only carried out two kinds of operations on the stack: the frame as a unit onto the stack and out the stack operations.
We know that a thread is being executed is called the current method of this thread. We may not know, the current method uses the frame as the current frame. When the thread to activate a Java method, JVM will be in the thread's Java stack into a frame where the new pressure. This frame will naturally become the current frame. In this method, during the implementation, the frame will be used to save the parameters, local variables, intermediate calculations and other data. The principle of frame here and compiled a record of the activity concept is the same.
This distribution mechanism from the Java point of view, the stack can also be understood that: Stack (Stack) is the operating system in the establishment of a process or when the thread (in support of multi-threaded operating system is thread) for this thread-storage area established , the region with advanced post-out features.
Java applications are unique for each one corresponds to a JVM instances, each corresponding to a unique instance of a heap. Application is running in the created instance or an array of all classes are placed in the heap by the application of all of the threads to share. With the C / C + + different, Java heap memory is allocated is automatically initialized. Java, all objects are heap allocated storage space, but a reference to this object is allocated in the stack, that is in the establishment of an object from the two places are allocated memory, the memory allocated in the heap real the establishment of this object, but the memory is allocated in the stack is just a pointer pointing to the heap object (reference) only.
Java stack and the heap are used to store the data in the Ram in place. With the C + + different, Java automatically manages the stack and heap, the programmer can not directly set the stack or heap.
Java run-time heap is a data area, the class of an object from which the allocation of space. These objects through the new, newarray, anewarray and multianewarray commands such as the establishment, they do not require code to explicitly release. Heap is responsible for garbage collection, the heap advantage is that you can allocate memory dynamically size, survival do not have to tell the compiler in advance, because it is dynamically allocated at run-time memory, Java's garbage collector will automatically be taken away these is no longer used data. But the disadvantage is that due to dynamically allocate memory at run time, access a slower pace. java objects and arrays are stored in the heap.
The advantage of the stack, access speed faster than the heap, behind registers, stack data can be shared. But the disadvantage is that there is data in the stack size and survival must be determined, the lack of flexibility. The main storage of the stack some of the basic types of variables (, int, short, long, byte, float, double, boolean, char) and object references.
Stack has a very important particularity is that there is data in the stack can be shared. Suppose we also define:
int a = 3;
int b = 3;
The compiler first deal with int a = 3; First, it will be in the stack to create a variable for a reference, and then check whether the stack 3 of this value, if not found, it will be three storage come in, and then a point to 3. Then deal with int b = 3; in creating finished b, reference variables, because the stack already has three this value, they put a b directly to 3. Thus, there are points a and b at the same time three situation. At this time, If we make a = 4; then the compiler will re-search the stack if there are four values, if not, will come in four storage and make it a point to 4; If you already have, then point to this address directly into a . Therefore, a change will not affect the value of b value. Should pay attention to such data sharing and a reference to two objects at the same time pointing to an object of this sharing is different, because this situation a modification does not affect the b, which is done by the compiler, which is conducive to save space. The one object reference variable is modified the internal state of this object will affect another object reference variable.
ps: on the c + + memory allocation
By a C / C + + compiled program memory is divided into the following sections
1, the stack area (stack) - is automatically assigned by the compiler release of stored function parameters, local variables, values, etc.. Their mode of operation similar to the data structure in the stack.
2, the heap area (heap) - generally assigned by the programmer to release, if the programmer does not release the program at the end may be recovered by the OS. Note that with the heap data structure is very different modes of distribution is similar to the list Dao Shi, He He.
3, Global Area (static area) (static) -, global variables and static variables are placed in a storage, and initialize the global variables and static variables in a region uninitialized global variables and non-initial The static variables of another one in the adjacent area. - After the end of the process system to release
4, text constant area - a constant string that is placed here. Program released by the system after the end of
5, the program code area - the binary code stored function body.
Second, an example program which is a senior writing, very detailed
/ / main.cpp
int a = 0; Global initialization Area
char * p1; whole area is not initialized
int b; stack
char s  = "abc"; stack
char * p2; stack
char * p3 = "123456"; 123456 \ 0 in the constant area, p3 in the stack.
static int c = 0; global (static) initialization Area
p1 = (char *) malloc (10);
p2 = (char *) malloc (20);
The allocation has come 10 and 20 bytes of the region in the heap area.
strcpy (p1, "123456"); 123456 \ 0 on the constant area, the compiler may be pointed to it with p3, "123456" optimization into one place.