What is the difference between stack and stack? Why is stack speed fast and stack speed slow?

Source: Internet
Author: User
What is the difference between stack and stack? Why is stack speed fast and stack speed slow? Both stacks and stacks are places where Java is used to store data in Ram. Unlike C ++, Java automatically manages stacks and stacks, and programmers cannot directly set stacks or stacks.
The Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime.
The advantage of stack is that the access speed is faster than that of stack, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variables (, Int, short, long, byte, float, double, Boolean, char) and object handles.
A very important feature of stacks is that data in stacks can be shared. Suppose we define both:
Int A = 3;
Int B = 3;
The compiler first processes int A = 3. First, it creates a reference with the variable A in the stack, and then finds whether the value 3 in the stack exists. If no value is found, store 3 and point A to 3. Then process int B = 3. After the referenced variable of B is created, B is directed to 3 because there is already 3 in the stack. In this way, both A and B point to 3 at the same time.
At this time, if A is set to 4 again, the compiler will re-search whether there are 4 values in the stack. If not, it will store 4 and make a point to 4; if yes, direct a to this address. Therefore, changing the value of A does not affect the value of B.
Note that the sharing of data is different from the sharing of two objects pointing to one object at the same time, because the modification of a does not affect B, which is completed by the compiler, it facilitates space saving. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.
String is a special packaging data. Available:
String STR = new string ("ABC ");
String STR = "ABC ";
The first method is to use new () to create an object, which is stored in the heap. Each call creates a new object.
The second is to first create a string class object in the stack to reference the variable STR, and then find whether the stack contains "ABC". If not, store "ABC" into the stack and point STR to "ABC". If "ABC" already exists, direct STR to "ABC ".
Use the equals () method to compare the values in a class. Use the = method to test whether the references of the two classes point to the same object. The example below illustrates the above theory.
String str1 = "ABC ";
String str2 = "ABC ";
System. Out. println (str1 = str2); // true
It can be seen that str1 and str2 point to the same object.
String str1 = new string ("ABC ");
String str2 = new string ("ABC ");
System. Out. println (str1 = str2); // false
The new method is used to generate different objects. Each time one is generated.
Therefore, the second method is used to create multiple "ABC" strings, and only one object exists in the memory. this method is advantageous and saves memory space. at the same time, it can improve the program running speed to a certain extent, because the JVM will automatically decide whether to create a new object based on the actual situation of the data in the stack. For the code of string STR = new string ("ABC");, a new object is created in the heap, regardless of whether the string value is equal, whether it is necessary to create a new object, this increases the burden on the program.
On the other hand, NOTE: When we define classes using formats such as string STR = "ABC";, we always take it for granted that the STR object of the string class is created. Worry trap! The object may not be created! Instead, it may only point to a previously created object. Only by using the new () method can a new object be created every time.
Because of the immutable property of the string class, when the string variable needs to change its value frequently, you should consider using the stringbuffer class to improve program efficiency.
========================================================== ========================================================== ==========================================
The Java virtual machine provides the runtime environment. The most important resource in the runtime environment is the runtime data zone.
The runtime data zone is the memory area allocated by the operating system for Java Virtual Machine processes, which is governed by virtual machines. At the same time, this area is divided into several sub-areas,
It mainly includes the heap, method, and java stack.
Store objects in the heap area,
Type information of the class is stored in the method area. The type information includes static variables and method information. The method information includes the bytecode of all methods of the class.

Stack has a feature. It comes out first, and the method is being executed in the process,
For example

Public void (){
B ();
}
Public void B (){
C ();
}
Public void C (){

}
Assume that a program only calls the () method (the stack is empty at the beginning ):

When running the program, run the () method first. Because method A calls Method B (), the () method is interrupted because method A () is not executed completely, put it into the stack for temporary storage (because the stack was originally empty, method A is pushed into the stack and located at the bottom of the stack), execute the method B, when Method B calls the C () method, the execution of Method B is interrupted and the method B () is pushed to the stack. Because method A already exists in the stack, after B is pushed in, it is located on top of a; after the C () method is executed, go to the stack to find the method at the top of the stack. Check that method B is used, and method B continues to execute, after the execution, go to the stack to find the method at the top of the stack. At this time, there is only one method in the stack, that is method A. Method A continues to execute. After Method A is executed, there is no way in the stack, and the program is running.

Suppose the column below is a stack.
-----------------------------------
|
| B () | --- the method closest to the stack port is first pop for execution.
| A () | --- the method at the bottom of the stack is finally executed
---

========================================================== ========================================================== ========================================
The Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new and newarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. However, the slow access speed is due to the need to dynamically allocate memory during runtime.
The advantage of stack is that the access speed is faster than that of stack, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variables (, Int, short, long, byte, float, double, Boolean, char) and object handles.

The basic data type is stored in the "stack", the object reference type is actually stored in the "Heap", and only the address value of the referenced memory is retained in the stack.
========================================================== ========================================================== ======================================
The size of memory occupied by data in the stack is determined during compilation. For example, if an int type occupies 4B, the variable address is well computed, so the allocation and destruction and access speed are relatively fast.
The size of memory occupied by the data in the heap is usually uncertain during compilation and can be known during runtime. Therefore, the address is only calculated during runtime and the memory size occupied during runtime may change, therefore, data allocation, destruction, and access are inconvenient and slow.
========================================================== ========================================================== ==========================================
The stack can contain addresses, and the stack can contain data or addresses (as you can imagine, things in the stack may point to other places)
Each address points to the given data. Otherwise, there is no need to exist. In the same way, if the data in the heap is not pointed to by a pointer, there is no need to exist, so when OBJ = NULL, the memory is released.
The advantage of Java is that there is no pointer, and all data transmitted in Java is passed for reference. Unlike C ++, the address can be passed. For example, the results of pointers P ++ and p + 1 are completely different.
========================================================== ========================================================== ==========================================
Stack is allocated space during compilation, while stack is dynamically allocated (space allocated during runtime), so the stack speed is fast.
The CPU has a dedicated register (ESP, EBP) to operate the stack. The stack uses indirect addressing. Stack faster. Http://java-true.iteye.com/blog/1181663

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.