I recently spent some time reading the previous 1-9 chapters of this classic book on java virtual machines. I feel like this is a very good book. As the author said, I have introduced in detail all aspects of Java virtual machines, it can be used as a reference tool. Below are some notes and understandings.
This book consists of 20 chapters and 1st chapters; 2nd to 5th chapters describing the Java architecture; 6th Chapter introducing the class file structure generated by Java compilation; chapter 7th describes the type lifecycle during running, including the loading, connection, initialization, and uninstallation processes. Chapter 8th goes deep into the Java connection model and describes how Java types are connected; chapter 2 introduces several different technologies of garbage collection. From Chapter 1 to Chapter 2, Chapter 4 describes the instruction sets of Java virtual machines and can be used as a tool book for reference.
The biggest feature of the Java architecture is platform independence. It uses virtual machines to compile Java applications (class files generated by compilation) read to the virtual machine and explain the run (this is a method, and there is also the JIT method, that is, just-in-time instant compilation and running ). This idea of Java virtual machine is also a manifestation of the concept of computer hierarchy, and brings about the benefits of isolation and decoupling, so we can see that it brings the benefits of platform independence.
However, the platform independence also gives Java other advantages, namely Network Mobility, that is, it can pass its program files on the network and run them everywhere. Of course, this is not determined by platform independence. At the same time, because of its compact class file structure design, the file size is usually relatively small, facilitating network transmission.
The disadvantage is the low efficiency, which is particularly evident in earlier versions. Now, due to the development of Java itself, its performance is gradually optimized, and the machine performance is constantly improving, it enables rapid development of upper-layer applications with low performance requirements.
If you want to use system APIs or other program language interfaces in Java, Java provides JNI (Java Native Interface) as the interface for interaction between the two. However, Java programs that use these interfaces have additional costs in portability and need to be transplanted to different systems.
Although the Network Mobility of Java programs has brought a lot of convenience, it also introduces a very important problem, that is, security. The program can run everywhere, which means that it cannot run anywhere. Java architecture has done a lot of work in security. The Sandbox Model of the earlier version 1.0 and the code signature authentication of Version 1.1 to the fine-grained access control model of version 1.2 are relatively complete. The Sandbox Model is too strict and many operations cannot be executed. Code signature authentication is feasible, but the granularity is too coarse. The Code either accepts all or rejects all; the fine-grained access control model is suitable and provides detailed access control. I will not go into details here. I just want to know that users can customize access control policies, you can inherit some security policy types in the Java architecture and load them to the Security Policy Manager at the beginning of the program. For specific usage, refer.
The structure of the Java class file is designed very compact, and its concept is clear and simple. It is described in a type table and reference in the whole article. The syntax structure is very simple and clear.
The architecture of Java Virtual Machine generally includes the Class Loader subsystem, method zone, heap zone, stack zone and execution engine. The class loader is responsible for loading connection and management type information, Loading Method bytecode in the method area, storing object data in the heap area, and the value of some stack reference variables during function calling in the stack area, the execution engine parses and executes Java commands.
A Java life cycle includes the following processes: Loading, verification, preparation, parsing, initialization, and uninstallation. The loading process simply loads the type information from the file. Verification is to verify the type of security, integrity and other information. In the preparation phase, allocate memory space for the class variables of the type and assign the default value to the type (not the value specified by the program, and no type-related code has been run at this time ). As the name suggests, the parsing process refers to reference fields in the class file of the type, such as other types referenced, referenced methods or fields, replace these symbolic references with direct references (this step is a bit like the connection step in the C ++ compilation process, but it is executed instantly ). Initialization is to correctly assign values to the class variables and run the static constructor of the class (if it exists or is generated by the compiler ). The unload type does not usually occur. Unless a user-defined type loader is used, the unload may occur. The unload mechanism is similar to garbage collection.
Java type connections (the parsing process described above) are dynamically implemented, that is, a type can be connected only when used. What's more, the method connection can be further delayed until it is used (similar to the dynamic link library in C ++ ). The parsing process has been described earlier. The parsing of various constant pool types is mainly described in the book. You Need To Know more information before reading. In addition, you need to pay attention to the load constraints. This mechanism is introduced to spoof users to write malicious code to read the value of the private variable of the type object. I think this is actually a patch-based solution proposed after the custom type loader is provided. I have no in-depth understanding of it and I don't know whether there is a better and more perfect solution.
Garbage collection is a major feature of Java. For Java programmers, it does not require programmers to pay attention to the application and release of memory, all of which are processed internally by virtual machines. This can reduce the program development cycle and the number of bugs, so that programmers can focus more on the upper-layer logic. The benefits are self-evident. This is also one of the reasons why many applications on the server use Java programs (of course, the server performance is strong enough to ignore the lack of performance caused by Java programs ). The disadvantage is obvious, and the object release time is unknown. Therefore, the correctness of the program cannot depend on the object structure. It seems that there is a complementary mechanism to make up for this defect later, but it is also a patch.
Because virtual machines determine the structure object, more complexity is introduced. For example, some other objects or even yourself may be used in a user-defined destructor, so they may be revived during the Destructor process. Java provides detailed State constraints and rules to ensure that objects can be released.
Several garbage collection algorithms are mentioned in the book. The most basic reference count is basically discarded because it cannot process cyclic references. Tracking and collection uses the flag-clear method, that is, traversing from the object reference forest (because there are not one root object, so the forest is not a tree) for marking, and then clearing the unlabeled, this is a basic algorithm that can be used. Of course, the performance is poor. Another type is the copy algorithm, which divides the heap space into two parts and uses only half each time. When the heap space is used up, copy the object by referencing the forest and then clear it. This method does not need to be marked, but it will always copy existing objects. The optimized algorithm is to collect objects by generation and classify objects according to the existing time, separate garbage collection for different classes. This sub-governance idea can be further extended. Different generations use different algorithms, namely adaptive algorithms.
Finally, the book introduces the train algorithm used by Sun's hotspot virtual machine at that time, which is an algorithm for garbage collection in the same generation. The optimization goal of this algorithm is to reduce the destructive effects of garbage collection (that is, the impact on normal program running. Some previous algorithms need to stop running programs at a certain time for garbage collection ). The algorithm makes more fine-grained distinction on memory, and also uses a memory set (extra space) to accelerate processing, which improves the overall collection efficiency and can be recycled in segments, only one part is recycled at a time.
The book also introduces the reference mechanism of Java, which is not easy to understand. The so-called strong touchable reference (that is, object reference in general) soft can touch the reference, weak can touch the reference and the shadow can touch the reference. We only know that the following three methods are used to standardize mappings and other mechanisms, such as hashmap. In my opinion, it is not perfect. The introduction of this mechanism is basically for a certain feature. I don't know what else to use? Or I got it wrong. please correct me.
Well, I have read so much about it, but I haven't read the instruction set. Let's take a look at it when necessary. My focus is on the architecture. Next I will look at the Framework Structure of. Net to understand how JIT works.