/ Text 14ET
The content of the fourth chapter is about memory allocation and initialization. It has brought a vague point in my past learning: What is a stack storage (STACK)? What is a stack storage? Difference? Turning a lot of information, make up this lesson, I feel very useful.
2.1 Memory Allocation Policy The memory allocation of the program is three strategies according to the sympathy of the program, which is static, stake, and stack. Static storage assignment refers to the time of compile time to determine each data objective The storage space requirements of the runtime, thus assigning fixed memory spaces when compiling. This allocation policy requires no change in variable data structures (such as variable groups) in the program code, nor is it allowed The structure of the set or recursive occurs because they all lead to the compiler that cannot calculate the accurate storage space requirements. Stack storage allocation can also be called dynamic storage allocation, which is implemented by a stack of stacks. And Static Storage Assignment In contrast, in the stack storage scheme, the program is fully unknown when compiling the data area, only when running, but it must be known when entering a program module in the run, must know the program module The data area size can allocate memory. Like the stack we are well known in the data structure, the stack storage allocation is assigned in accordance with the principles of advanced. Static storage allocation requires that you can know the storage requirements of all variables during compile, and the stack storage allocation requires that all storage requirements must be found at the entrance of the process, and the stack storage assignment is dedicated to the entrance of the module when compiling or runtime. Memory allocation, such as variable length string and object instances that store requirements, such as variable length string and object instances. The heap is composed of a large piece, and the memory in the heap can be assigned and released in any order.
2.2 Compared with the stack and stack Summary from the textbook of the compilation principle, except static storage assignments, it is very static and difficult to understand, the following is static storage allocation, focus on the stacks and stacks: from the stacks and stacks The functions and functions are common, and the stack is mainly used to store the object. The stack is mainly used to execute the program. This difference is mainly due to the characteristics of the stack and stack: in programming, such as C / C In all methods, all methods are made through the stack, all local variables, and form parameters are allocated from the stack. In fact, it is not allocated, just use it from the top of the stack, just like the conveyor belt in the factory, Stack Pointer automatically guides you to the place, you have to do it, just put things down. When exiting the function, modify the stack pointer to destroy the contents of the stack. This mode is the fastest, of course, it is necessary to run the program. Need to note that when allocation, such as a program that is about to call When the module assigns the data area, you should know the size of this data area in advance, that is, although the assignment is performed at the program run, how much the size is determined, the "size" is Determined time, not running. Stack is the application of the application to assign the operating system when the application is running, since the memory allocation from the operating system management, so it takes time when allocating and destroying, so use the heap The efficiency is very low. But the advantage of the heap is that the compiler does not have to know how much storage space is to be allocated from the heap, and you don't have to know how long the store is stopped in the stack, so it will be larger. Flexibility. In fact, object-oriented polymorphisms is essential, because the storage space required for polymorphic variables can only be determined after the object is created, in C , when you create an object, only You need to prepare the relevant code with the new command. When performing these code, the data is saved automatically in the stack. Of course, in order to achieve this flexibility, you will inevitably pay a certain price: I will take a longer time when allocating the storage space! This is the reason why the efficiency of our just said, it seems that Lenin comrade said is good, and the advantages of people are often the shortcomings of people. The shortcomings of people are often the advantages (halo ~).
2.3 Piles in the JVM, the JVM is a stack-based virtual machine. JVM allocates a stack for each newly created thread. That is to say, for a Java program, its run is done by the operation of the stack. of. The stack saves the state of the thread in a frame-based unit. JVM only performs two operations on the stack: stack and out of the stack in frames. We know that the method in which a thread is performing is called the current method of this thread. We may not know that the current method is called the current frame. When the thread activates a Java method, the JVM will press a frame in the Java stack of the thread. This frame is naturally a current frame. This frame will be used to save parameters, local variables, intermediate computing processes, and other data. This frame is similar to the concept of activity records in the compilation principle. From From this distribution mechanism, the stack can be understood by the stack: stack is the storage area established for this thread when the operating system is established or thread (thread in the operating system that supports multi-thread). This area has advanced features. Each Java application is only a JVM instance, and each instance is unique to a heap. All class instances or arrays created in the run are placed in this heap and shared by applying all threads. Unlike C / C , allocating stacks in Java are automatically initialized. All objects in Java are allocated in the heap, but the reference to this object is allocated in the stack, which means that the memory is allocated from two places when establishing an object, and the memory is actually allocated in the heap. Establish this object, and the memory allocated in the stack is just a pointer to this heap object (reference).
2.4 GC thinking Java Why is Java? The existence of JVM is of course a reason, but some people say that in Java, in addition to the simple type (int, char, etc.) data structure, all allocated memory in the heap (So Java) Everything is an object), which is one of the reasons for the slower program. My thoughts are (should be said to represent Tij's point of view), if there is no Garbage Collector (GC), the above statement is to be established. The stack is not stack is a continuous space, there is no way to count the memory allocation of the stack to have the same stack. The conveyor belt speed, because who will give you a huge stack of space, let you get a new space from the pile? At this time, the GC stands out to solve the problem. We all know that GC is used to clear memory garbage. In order to use the space for sale, GC also takes another important task, that is to let the memory allocation in Java and the memory allocation of the stack in other languages, because the speed is almost a word For Java's disease. To achieve this, you must make the assignment of the pile can also be used as a conveyor belt. In addition, GC is responsible for clearing the Garbage, but also responsible for the object in the heap. To transfer them to a pure space away from Garbage, it is as compact like a stack, so heap pointer can conveniently point to the starting position of the conveyor belt, or say unused space, for the next It is necessary to allocate memory object "Guidelines". Therefore, it can be said that the garbage collection affects the speed of the object, sounds strange, don't be right? How does the GC find all survival objects in the heap? When establishing an object, assign the memory that actually establishes this object in the stack, and assigns a pointer to this heap object (reference) in the stack, then you can find this reference only in the stack (or possibly in a static storage area). After tracking all survive objects. After found, GC moves them from a plumbing block into another stack of blocks, and arranges them one by one, just like the above, simulates one The structure of the stack, but it is not an advanced allocation, but can be allocated, and if the speed can be guaranteed, ISN't it great? However, Comrade Lenin said that people's advantages are often the disadvantages. Human shortcomings are often the advantages of people (re-halo ~~) .GC () runs to occupy a thread, which is a defect that reduces the performance performance of the program, let alone this thread will also cover the memory in the stack Not only that, as mentioned above, the living objects in the heap are moved, then all references to these objects have to be re-assigned. These overhead will result in a decrease in performance. This is a long, GC () advantage Whether the benefits have been caught in its shortcomings, I don't have much experience, Bruce Eckel is a supporter of Java, Wang Po sells melon, and I can't believe. Personal feelings are, Java is still very slow It needs time to develop. The above experience is that I have seen Tij.3rdedition.Revision4. The fourth chapter of 0, the content and the front are somewhat different. I have never seen the Chinese version of Houjie, but I think that on the key issue, the original Tij is indeed worth reading. So cooperated with Chinese Learning is a good choice. I can only calculate a Java's beginner, I didn't expect such a topic, but I was concerned about such a number of people, and I was happy, I also determined to write everything below. But This one is over, I should prepare to go to the United States, if successful, then wait until August 27 graduate school graduate school, there is time to start researching the next chapter, I hope I can get more from the original version. A little experience. June Beijing, I hope good luck.
(Note: The stacks and stacks in the article refer to stack and stacks to distinguish between