Tij Reading Notes (2)

Source: Internet
Author: User

/文1414et

Chapter 4 covers memory allocation and initialization. The learning in this chapter brings out a fuzzy point in my previous learning: What is heap )? What is stack )? What is the difference? After reading a lot of materials and completing this lesson, I feel very useful.

2.1 Memory Allocation Policy
According to the compilation principle, there are three policies for memory allocation during program running: static, stack, and heap.
Static Storage Allocation refers to the ability to determine the storage space requirements of each data target at runtime during compilation. Therefore, a fixed memory space can be allocated to each data target during compilation. this allocation policy requires that the existence of a variable data structure (such as a variable array) or nested or recursive structure is not allowed in the program code, because they both cause compilation programs to fail to calculate accurate storage space requirements.
Stack-based storage allocation, also known as dynamic storage allocation, is implemented by a stack-like running stack. in contrast to static storage allocation, in stack-based storage solutions, the program's requirements for data areas are completely unknown during compilation and can only be known at runtime, however, when entering a program module during running, you must know the size of the Data zone required by the program module to allocate memory for it. like the stack we are familiar with in the data structure, stack-based storage allocation is distributed based on the principle of first-in-first-out.
Static storage allocation requires that the storage requirements of all variables be known during compilation, and stack-based storage allocation requires that all storage requirements be known at the entrance of the process, heap Storage allocation is specifically responsible for the memory allocation of data structures that cannot be determined at the module entrance during compilation or runtime, such as variable length strings and object instances. the heap consists of a large part of available blocks or idle blocks. The memory in the heap can be allocated and released in any order.

Comparison between 2.2 heap and stack
The above definition is summarized in the textbook on compilation principles. In addition to static storage allocation, all of them seem dull and hard to understand. The following describes static storage allocation and how to compare stacks and stacks in a centralized manner:
Compared with the functions and functions of stacks, stacks are mainly used to store objects and stacks are mainly used to execute programs. this difference is mainly determined by the features of the stack and stack:
In programming, for example, in C/C ++, all method calls are performed through stacks, and all local variables and formal parameters are allocated memory space from stacks. In fact, there is no allocation, just use it from the top of the stack, just like a conveyor belt in the factory (Conveyor Belt), stack pointer will automatically guide you to the place where you put things, all you have to do is put things down. when you exit the function, you can modify the stack pointer to destroy the stack content. this mode is the fastest, of course, used to run the program. it should be noted that, during the allocation, for example, when assigning a data zone to a program module to be called, the size of the Data zone should be known in advance, that is to say, although the allocation is performed while the program is running, the size of the allocation is fixed, and the "size" is determined during compilation, not at runtime.
Heap requests the operating system to allocate memory when the application is running. Because the memory allocated is managed by the operating system, it takes time to allocate and destroy the heap, therefore, the efficiency of using the heap is very low. however, the advantage of heap is that the compiler does not have to know how much storage space is allocated from the heap or how long the stored data will stay in the heap. Therefore, storing data in a heap gives you more flexibility. In fact, heap memory allocation is essential for object-oriented polymorphism, because the storage space required for Polymorphism variables can be determined only after the object is created at runtime. in C ++, when creating an object, you only need to use the new command to compile the relevant code. When the code is executed, data is automatically saved in the heap. of course, to achieve this flexibility, you must pay a certain price: It will take longer to allocate storage space in the heap! This is exactly the reason why we have just said that the efficiency is low. It seems that comrade Lenin is good at saying that the advantages of people are also the disadvantages of people, the disadvantage of a person is often the advantage of a person (dizzy ~).

2.3 heap and stack in JVM
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 operation is done through stack operations. The stack stores the thread status in frames. JVM only performs two types of operations on the stack: frame-based stack pressure and outbound stack operations.
We know that the method being executed by a thread is called the current method of this thread. We may not know that the frame used by the current method is called the current frame. When a thread activates a Java method, JVM will press a new frame into the thread's java stack. This frame naturally becomes the current frame. during the execution of this method, this frame will be used to save parameters, local variables, intermediate calculation processes, and other data. this frame is similar to the activity record concept in the compilation principle.
From the perspective of Java's allocation mechanism, the stack can be understood as follows: a stack is a process or thread created by the operating system (a thread in an operating system that supports multithreading) the storage area created for this thread has the advanced and later features.
Each Java application corresponds to only one JVM instance, and each instance corresponds to only one heap. All the class instances or arrays created by the application during running are stored in this heap and shared by all the threads of the application. unlike C/C ++, heap memory allocation in Java is automatically initialized. In Java, the storage space of all objects is allocated in the heap, but the reference of this object is allocated in the stack. That is to say, the memory is allocated from both places when an object is created, the memory allocated in the heap actually creates this object, and the memory allocated in the stack is just a pointer (reference) pointing to this heap object.

2.4 GC thinking
Why is Java slow? The existence of JVM is certainly one reason, but some people say that in Java, apart from the data structure of simple types (INT, Char, etc, others are allocating memory in the heap (So everything in Java is an object), which is also one of the reasons for slow programs.
My idea is (it should be said to represent tij's point of view). If there is no garbage collector (GC), the above statement is true. the heap is not like the stack is a continuous space, and there is no way to expect the heap's memory allocation to have a conveyor speed like the stack, because who will sort out the huge heap space for you, what makes you get new space from the heap with almost no latency?
At this time, the GC station came out to solve the problem. we all know that GC is used to clear the memory garbage and free up space for the heap for the program. However, GC is also responsible for another important task, it is to make the heap memory allocation in Java as fast as the memory allocation in other languages, because the speed problem is almost a public criticism of Java. to achieve this goal, you must make the heap allocation the same as the conveyor belt, so you don't have to worry about finding free space. in this way, GC is not only responsible for clearing garbage, but also responsible for organizing the objects in the heap and moving them to a pure space away from garbage without intervals, which is as compact as in the stack, in this way, heap pointer can easily point to the starting position of the conveyor belt, or an unused space, and "Guiding Direction" for the next object needing to allocate memory ". so it can be said that garbage collection affects the creation speed of objects. It sounds strange, right?
Then how does GC find all the surviving objects in the heap? As mentioned above, when an object is created, allocate the memory of the object actually created in the heap, and assign a pointer (reference) to the object in the stack ), as long as the reference is found in the stack (or in the static storage area), all the surviving objects can be tracked. after finding them, GC will move them from one heap block to another heap block and arrange them one by one, as we said above, simulate a stack structure, but it is not an advanced distribution, but can be allocated at will. When the speed can be guaranteed, isn' t it great?
However, comrade Lenin said that the advantages of people are often also the disadvantages of people. The disadvantages of people are also the advantages of people (then dizzy ~~). GC () runs to occupy a thread, which is itself a defect to reduce the performance of the program, not to mention the thread also needs to overwrite the memory in the heap. not only that, as mentioned above, all references to the objects in the heap must be assigned a value. these overhead will cause performance degradation.
I am not familiar with the advantages of GC (). Bruce Eckel is a Java supporter, you cannot send all emails. my general feeling is that Java is still very slow and it still takes time to develop.

I have read tij.3rdedition. the content obtained after Chapter 4 in revision4.0 is somewhat different from the previous one. I have not read the Chinese version of Hou Jie, but I think that the original tij is indeed worth reading on key issues. therefore, it is a good choice to study with the Chinese version.
I can only be a beginner of Java. I did not expect such a question, but I have received so many people's attention. I am delighted to try my best to write every article below. however, after this article is complete, I should prepare a visa to go to the United States. If it succeeds, I will wait until the start of the CS graduate school in August 27 before I can start to study the next chapter, I hope I can get more experience from the original version.
May June Beijing, hope good luck.

(Note: Both stack and stack refer to the need to distinguish Stack from Stack)

 

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.