In-depth understanding of Java virtual machine - learning perception and notes

This article is transferred from: http://www.cnblogs.com/wtzbk/p/7985156.html

1. Why learn Java Virtual Machine?

      Here we use examples to illustrate why we should learn Java virtual machine. In fact, this question is the same as why we should learn data structures and algorithms. I used to be afraid to deal with the problem of memory overflow, because I don't know why he has this problem. When I understand the garbage collection algorithm after reading this book, and how the JVM helps us deal with GC, this time when it appears When I had this problem, I knew that I needed to find the GC Root, or check the GC log, to find the root cause of the problem, so that I could deal with these problems. In the past, when I understood overloading and refactoring, I only understood it on the surface. When I read this book, I realized that these things are generated and processed when the method is called. In addition, there is a new experience in the end, etc. A series of problems, if you are still entangled in some problems, why are you dealing with them in this way, then you can go to see the Java virtual machine, you may have a different perception, the above is the reason why you should learn the Java virtual machine, there may be some The explanation is not very comprehensive, I would like to elaborate on this issue in terms of perception.

Second, perception?

      In fact, it's not really an insight, it's just a deeper understanding of some issues. Let's talk about GC here. To discuss this issue, we need to start from four aspects:

      1. How does the JVM allocate memory?

      2. How can we ensure correct recycling?

      3. Under what circumstances does JVM trigger GC and how to trigger GC?

  4. How to monitor and optimize GC?

 First start with the JVM memory distribution: The following figure is the JVM memory distribution diagram

 

 

 1. The thread counter is a small memory space used to specify the number of lines of bytecode executed by the current thread. Each thread counter is private, because each thread needs to record the number of lines executed; explain it here Why does each thread need a thread counter? The multi-threading of the JVM is achieved by allocating execution time by thread switching. At any time, each processor will only execute the instructions in one thread. When the thread switches , in order for the thread to restore the correct position, each thread must have an independent thread counter, so as to ensure that the threads do not affect each other.

  Note here that if the thread execution is a Java method, the counter records the address of the virtual machine bytecode instruction; when the native method is executed, the counter instruction is empty; this memory area is the only memory area of ​​the Java virtual machine. There is no region specified for any OutOfMemoryError.

 2. Java virtual stack, which is also private to a thread. The life cycle is synchronized with the thread. When each method is executed, a stack frame will be created to store the local variable table, operand stack, dynamic link, method Information such as entry and exit, the process from the invocation of each method to the completion of the execution is a process from stack frame push to stack;

  Here is an explanation of the local variable table, which stores local variables related to methods, including basic data, object references, and return addresses. In the local variable table, only the long and double types occupy 2 local variable spaces (Slot, for a 32-bit machine, a Slot is 32 bits), and the others are 1 Slot. It should be noted that the local variable table is determined at compile time, and the space allocated for the method to run is completely determined in the stack frame and will not change during the life cycle of the method. For this part, I want to wait for the next blog. I want to talk about the execution process of bytecode in detail;

  The virtual machine stack specifies two exceptions. One is that the depth of the thread request stack is greater than the depth allowed by the virtual machine stack. At this time, a StackOverflowError exception will be thrown. If the Java virtual machine allows dynamic expansion of the virtual machine stack, When there is no way to allocate memory during expansion, an OutOfMemoryError exception will be reported;

    3. The native method stack is basically the same as the virtual machine stack, the only difference is that the virtual machine stack executes Java methods, and the native method stack executes native methods;

    4. Java heap, the heap area is the largest piece of memory managed by the Java virtual machine. The Java heap is a memory area shared by all threads and mainly stores instances of objects.

       When there is no memory in the heap to complete the instance allocation, and the heap cannot be expanded, an OutOfMemoryError exception will be thrown; the current virtual machine can be expanded;

   5. The method area, which is also a memory area shared by threads, stores class information, constants, static variables, and real-time compiled code data loaded by the virtual machine;

      The method area does not need to be physically continuous. You can choose a fixed size or an extended size. You can also choose not to implement garbage collection. The garbage collection of the method area is relatively small, which is why the method area is called a permanent area. , but the method area can also be reclaimed. This area is mainly for the unloading of constant pools and types; in the method area, it is also stipulated that when the method area cannot meet the memory distribution, an OutOfMemoryError exception will be thrown;

      Runtime constants are part of the method area. The constant pool is mainly used to store various literals and conforming references generated by compilation. Since the constant pool is part of the method area, an OutOfMemoryError exception is thrown when the constant pool has no memory space;

   6. Direct memory, not part of the virtual machine runtime, can directly access the memory outside the heap; so when the memory space cannot be dynamically expanded, an OutOfMemoryError exception will occur;

   The above is basically the content of JVM memory distribution. Simply understand that the water overflows. The entire space of the system is a large container. It is divided into different parts or buckets to share the entire capacity. When the bucket is not enough, it will overflow naturally. . To understand the distribution of memory areas, let's see how objects are allocated in the memory space?

  The Java object here refers to the object of reference type. Here, Student stu=new Student() is used as an example to access, Student stu is used as a reference object and exists on the Java virtual machine stack, and new Student() is stored in the Java heap, in the heap Record the information of the Student type, including the addresses of methods, interfaces, object types, etc., and the execution data of these types are stored in the method area;

  Here we need to explain the way of object access, mainly including two kinds of handle access and direct pointer access:

 1. Handle access is mainly divided into a handle pool in the Java heap. The virtual machine stack stores the address in the handle pool. The handle pool includes the address of the instance data of the object and the data of the object type. The basic distribution is as follows:

  

   2. Direct pointer access means that the virtual machine stack directly points to the object type pointer and the instance data of the object in the Java heap, and then the object type pointer points to the instance data of the object type in the method area, as shown in the following figure:

 HotSpot is the second access method. The advantage is that the access speed is fast, and the pointer overhead time is saved. The JVM memory distribution is basically introduced here. Next, how to ensure correct recycling?

  Recycling is an object that is no longer useful, so how do you judge that an object is useless? Here we need to briefly introduce two methods: reference counting method and reachability analysis algorithm;

  Here is a brief description of the reference counting method: add a reference counter to the object, increase the reference counter by 1 whenever there is a place, decrease the reference by 1 when the reference fails, and the counter is 0 and cannot be used; the disadvantage is that it cannot deal with the problem of direct mutual reference of objects , because the counter cannot be set to 0 after mutual reference, so it cannot be recycled;

  The reachability analysis algorithm, also known as GC Root, can be recycled when an object is not connected to any reference chain. The following are several places where GC Root objects are used in Java:

 

  

   The above objects are simply divided into two types: available and unavailable. Now Java expands the concept of reference:

  Knowing this, we basically understand how the JVM can recycle correctly. The next step is under what circumstances does the JVM trigger the GC and how does the GC trigger?

  The first question is easier to answer. Of course, when the memory space is insufficient, GC needs to be triggered. The GC recycling adopts the generational collection algorithm, which is mainly divided into young generation and old generation. Next, we will briefly introduce these two ways:

   Young generation: When an object is created, the memory allocation is first allocated in the young generation. Most objects are no longer used after they are created. The object quickly becomes unreachable, that is, the object is useless, because the garbage is cleaned up by the young generation. , so it is called Minor GC or Young GC.

   Old generation: If an object has survived long enough in the young generation without being cleaned up (that is, survived after several Young GCs), it will be copied to the old generation. The space of the old generation is generally larger than that of the young generation. Large, can store more objects, and the number of GCs in the old generation is less than that in the young generation. When the old generation runs out of memory, a Major GC, also called a Full GC, will be performed.

   After understanding what these two blocks mainly store, let's take a look at the overall structure of the GC and see the process of how an object is killed:

   

   1. When an object is created (new), it will first be created in the Eden area of ​​the young generation, until the GC, according to the reachability algorithm, to see if an object dies, and objects that do not die will be put into the young generation. In the Survivor area, the dead ones are directly killed by Minor GC;

   2. The objects entering the Survivor area are not safe. When the next Minor GC comes, it will check whether the objects in the Enden and Survivor storage object areas are still alive, and put them in another Survivor area;

   3. When the two Survivor areas are switched several times, they will directly enter the old age. Of course, it is not safe to enter the old age. When the memory space of the old age is insufficient, the Major GC will be triggered, and the dead ones will still be killed by Kill. ;

   Recommend one that is very funny to read: http://blog.csdn.net/sd4015700/article/details/50109939

   Next, we also need to talk about the GC algorithm: mark - clear, copy, mark - organize these three algorithms;

   

 

   

 

 

   After understanding the algorithm and GC memory distribution, we will introduce the garbage collector. I do not plan to introduce this part in words. In the third column, I will give my mind map of the book "In-depth Understanding of Java Virtual Machine". , the content is not very complete, I am sorting it out, but there is GC part of the content including various parameter configurations, you can download it to learn more about it;

    Finally, let’s talk about monitoring and optimization. After having the above knowledge, these will not be a problem. Therefore, if you want to do good things, you must first sharpen your tools. This is what I want to say. The rest is to operate the tools. I don’t think these are needed Introduction is also possible, of course I also recommend a blog: http://blog.csdn.net/renfufei/article/details/56678064

3. Conclusion

    Mind Mapping Tool: XMind

    Baidu cloud disk address: link: https://pan.baidu.com/s/1ge3eE2Z password: hox4 I have not completed this part, I am working on it, it should take about a week; if there is no Baidu cloud disk, you can Join my QQ group: 438836709, you can communicate and learn Java together. I also share many PDF books in the group. Anyway, everyone is welcome to join! Of course, I will also perfect this file and upload it to the QQ group! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325314223&siteId=291194637