The allocation of variable space in the Java program is mainly concentrated in three memory area: static memory, stack and heap. Stack The variables for storing basic types. The basic type is the Java language and the efficiency is retained. They store them in the form of automatic variables. The object's Reference can also be stored, but the object itself can never store it. Piles are the most important space allocation of Java, which is used to store all Java objects. Java uses the New keyword to dynamically allocate memory space on the pile. The stack in Java is managed by Java, which is transparent to the programmer. Static storage spaces are used to store variables that are declared STATIC, which can store the object's Reference but cannot store the object itself, which is the same as the stack. There is also a constant storage space for constant in the program. Java also includes a special Non-RAM storage. Data is completely active in the program, even if the program does not continue to execute, these data still exist. This mainly includes streaming objects and persistent objects.
In C , global variables and static variables allocate space in static memory zones; auto variables and local variables allocate space in the stack; and allocate space in the stack in the stack; C also has a constant storage space. The variable compiler in static memory and stack determines its survival period and automatically eliminates. Different from Java, the objects in C can be stored in the stack, such as a direct declaration, without using the keyword NEW object, such objects automatically call the default constructor, allocate space in the stack. In this case, the object must have a default constructor, otherwise it will report an error. The variable in the heap is to use new objects to define the programmer to manually eliminate. This is also a comparative headache in C . So in the use of the NEW keyword in C , it is not more cautious, not in Java, the object is New.
The stacks and stacks are two basic data structures. However, the stack is the data structure provided by the machine system, and modern computers provide support for stack structures on the underlying. The computer has a dedicated register pointing to the stack address, and has machine instructions that do direct the stack. Therefore, the efficiency of operation in the stack is high, but the supported data type is also limited. The high efficiency characteristics of the stack make it high. Function call is a typical example of the stack. The program will return the address stack when the function is called, and then jumps to the address of the called function. The function call is completed, the address spring will return, return to the main program. The variables stored in the stack must know their size and survival time in the compile period. In C / C , you can use alloca to dynamically allocate the stack, of course, is not encouraged.
The stack has an interesting feature that is, it is shared. For example, two variables are defined: int a = 3; int b = 3; the compiler will create a reference to a variable A, and then find the address that is 3. If you do not store the integration of integration 3, point a point to it. The definition of the B variable is that because there is already a 3 number in the now stack, the b is directly directed to the address where the 3 is located. This mechanism is different from Reference. When there is a change in A and B, it does not affect the other. For example, an = 4 statement is executed. First, the system finds no existing 4 in the stack. If there is a point to point to this address, you will allocate a new space store 4, then point to this new address.
The stack structure is generally implemented by a particular language library. For example, Malloc, Free et al., Is a function of management and maintenance of the heap structure. When the program gets a new memory space, first try to find the available memory space from the internal heap. If there is no memory space available, try to utilize the system call to dynamically add the memory size of the program data segment, the newly assigned space first The organic into the internal heap and then returns to the caller in an appropriate form. When the program releases the assigned memory space, this memory space is returned in the internal heap structure, which may be properly processed (such as incorporating other idle spaces into larger idle space) to make the next memory allocation application. This complex allocation mechanism is actually equivalent to a memory allocated buffer pool (cache). The definition of Chinese and external classes in Java is a bit complicated. Take String as an Example: About String Str = "ABC" internal work. Internal Java converts this statement to the following steps: (1) First define an object reference variable called String class: String Str; (2) Find the address of "ABC" in the stack If not, open the address of the "ABC" that is stored, then create a new String class object O, and points the O's string value to this address, and record this reference next to this address in the stack. Object O. If there is already an address of the value "ABC", the object O is found and the address of O is returned. (3) Point the STR to the address of the object O.