Stacks and heaps are the center of Java used to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set up stacks or heaps.
The Java heap is a run-time data area where objects from the class allocate space. These objects are established by directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to statically allocate memory size, life time without prior notification to the compiler, because it is in operation static allocation of memory, Java garbage collector will automatically take away these no longer use data. The flaw, however, is that the access speed is slower due to the static allocation of memory during operation.
The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. But the flaw is that the size of the data in the stack and the life time must be deterministic and lack of agility. The stack mainly registers some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object handle.
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 disposes of int a = 3; First it creates a reference to a variable in the stack, and then finds out if there is a value of 3 in the stack, and if it does not, it registers the 3 and then points a to 3. then Dispose int b = 3; After creating the reference variable of B, because there are already 3 values in the stack, point B directly to 3. In this way, A and B are both pointing to the condition of 3.
At this point, if the a=4 again, then the compiler will re-search the stack can have a value of 4, if not, then 4 out, and a point to 4, if already, then direct a point to the address. Therefore the change of a value does not affect the value of B.
It is important to note that this sharing of data with two object references also points to an object where this share is different, because the modification of this condition A does not affect B, which is done by the compiler, which facilitates space saving. An object reference variable modifies the external state of the object, affecting another object reference variable.
String is a special wrapper class data. Can be used:
- String str = new string ("abc");
- String str = "abc";
Two forms of creation, the first is to create new objects with new (), which is stored in the heap. A new object is created each time the call is made.
The second is to create an object reference to the string class in the stack str, and then find the stack is not registered "ABC", if not, the "ABC" is stored in the stack, and the str point to "ABC", if there is already "ABC" directly to the "ABC".
If the values outside the class are equal, use the Equals () method; When testing the two wrapper class references can point to the same object, use = =, the following example illustrates the following theory.
- String str1 = "abc";
- String str2 = "abc";
- System.out.println (STR1==STR2); //true
You can see that str1 and str2 are pointing to the same object.
- String str1 =new String ("abc");
- String str2 =new String ("abc");
- System.out.println (STR1==STR2); //False
The new method is to generate different objects. Each time one is generated.
Therefore, the first way to create multiple "abc" strings, in memory there is only one object. This writing is advantageous and saves memory space. At the same time it can improve the speed of the program at a certain level, because the JVM will automatically according to the actual situation of data in the stack to make it necessary to create a new object. For the code of string str = new String ("abc"), it is necessary to create a new object in the heap, regardless of whether the string value is equal, and it is possible to create a new object, thereby easing the burden on the program.
On the other hand, note that when you define a class using a format such as String str = "ABC", you always want to assume, of course, that you have created the object str of the String class. Worry about traps! The object can not be created! And can just point to a previously created object. Only through the new () approach can you make a new object every time.
Because of the immutable nature of the string class, when a string variable needs to change its value frequently, it should think about using the StringBuffer class to improve the efficiency of the program.
The piecemeal care after application
Stack: As long as the remaining space of the stack is larger than the requested space, fragmented will provide memory for the program, otherwise the exception prompt stack overflow.
Heap: First of all should know that the operation is fragmented there is a record of the free memory address of the linked list, when the piecemeal receipt of the application of the program, will traverse the list, the first space is larger than the application space of the heap node, and then the node is removed from the list of idle nodes, and the node space allocated to the program, in addition The size of this allocation is recorded at the first address in this memory space, so that the DELETE statement in the code frees up the memory space correctly. In addition, because the size of the found heap node does not necessarily equal the size of the request, fragmentation automatically re-places the extra portion into the idle list.
Limit for size of application
Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the stack top address and the maximum capacity of the stack is piecemeal pre-defined, under Windows, the size of the stack is 2M (also can be 1M, it is a compile-time determination of the constant), if the application of space beyond the stack of space, will be prompted overflow. Therefore, the space can be obtained from the stack is small.
Heap: A heap is a data construct that extends to a high address, and is a discontinuous area of memory. This is due to the fact that fragmented is a free memory address that is stored with a linked list, which is naturally discontinuous, while the traversal direction of the list is addressed by the low address to the high address. The size of the heap is limited by the virtual memory that is valid in computer fragmentation. It can be seen that the space of the heap is more sensitive and comparable to the larger.
Comparison of application efficiency:
Stacks are automatically allocated from a piecemeal, faster speed. But programmers can't control it.
Heap is the memory allocated by new, the normal speed is slower, and prone to memory fragmentation, but the most convenient to use.
In addition, under Windows, the best way is to use VirtualAlloc to allocate memory, he is not in the heap, nor in the stack is directly in the process's address space to keep a fast memory, although the most inconvenient to use. But the speed is fast, also the most sensitive.
Storage content in heaps and stacks
Stack: In a function call, the first stack is the address of the next instruction in the main function (the next executable statement of the function call statement), and then the parameters of the function, in most C compilers, the arguments are left-to-right and then the local variables in the function. Note that static variables are not in the stack.
When the function call is complete, the local variable is first out of the stack, then the parameter, the last stack pointer to the end of the address, that is, the main function of the next instruction, the program from the point to continue working.
Heap: Normal is the size of the heap that is stored in the pile's head with a single byte. The details in the heap are laid out by programmers.
Comparison of access efficiency
- char S1 = "AAAAAAAAAAAAAAA";
- Char *s2 = "bbbbbbbbbbbbbbbbb";
AAAAAAAAAAA is assigned at the hour of operation;
And BBBBBBBBBBB is determined at compile time;
However, in subsequent accesses, the array on the stack is faster than the string that the pointer points to (for example, a heap).
Like what:
- void Main ()
- {
- char a = 1;
- Char c[] = "1234567890";
- Char *p ="1234567890";
- A = c[1];
- A = p[1];
- Return
- }
The corresponding assembly code
- 10:a = c[1];
- 00401067 8A 4D F1 mov cl,byte ptr [ebp-0fh]
- 0040106A 4D FC mov byte ptr [ebp-4],cl
- 11:a = p[1];
- 0040106D 8B-EC mov edx,dword ptr [ebp-14h]
- 00401070 8A mov al,byte ptr [edx+1]
- 00401073 FC mov byte ptr [ebp-4],al
The first reads the elements in the string directly into the register CL, while the second one reads the pointer value into edx, based on
EdX reads the characters, apparently slowly.
Summary:
The difference between heap and stack can be seen in the following analogy:
The use of stacks like we go to a restaurant to eat, just order (take back the application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his benefits are fast, but less freedom.
Detailed introduction to stacks and stacks in Java