Write a summary of their own JVM

What JVM that? Why should I learn? How to learn best?

  •  java virtual machine, java code we write, the machine can recognize, it is a virtual machine to help us to complete.
  • Just as people or they would rely on the same planet, java code that want to live is dependent on JVM virtual machine. As for why to learn, like my life on earth scientists to study climate and geology to study, to study living things. Who study the Earth in order to better survival, improve quality of life. Then we study the meaning of JVM is to make our code more efficient operation of the quality of there.
  •  How to learn this, my suggestion is to see this part of Alibaba publication "a highly efficient code," the fourth chapter of this book enough. Alibaba is no doubt that China java camp big brother, look at those big cattle is how to talk JVM virtual machine.

 If you do not have knowledge JVM virtual machine before, I feel that this article will be difficult to read, just like I did when JVM basics, as painful to read "real JVM virtual machine" when. Persevered, pain in the front, in the back harvest. "Practically JVM virtual machine" This book is also really good. If you want to learn the system, I recommend this book.

  Not necessarily to recommend this book, I felt standing on the shoulders of giants is necessary, I think the most afraid of is not as good as others, do not learn from others.

  In the "code of efficient" This book, a total of five parts, presumably they think the most important part. I am also learning to expand around these parts. They are: bytecode; class loading mechanism; memory layout; object instantiation; garbage collection.

Part I: bytecode

  This part of my understanding is not sufficient, but I can say I understand it. Top mentioned, the code is written by us, then let the machine (hardware) to recognize is that bytecode. In other words, the source file has gone through a process to be able to identify the machine bytecode, compiler theory the process design. I will not elaborate.

 This whole process is as follows:

  Interested in their own learning compiler theory this course. Not interested to understand: we write code, we have gone through a process to bytecode JVM byte code is recognized. This process becomes compiler (javac completed). Why study this issue say the JVM bytecode it? Simple can be understood, we eat bread, bread from wheat processing is over, although we eat wheat, but wheat is processed. Bread from wheat feedstock to this process is to compile. It corresponds to bytecode bread. JVM is the equivalent of people.

Part II: class loading mechanism 

 So what is the class loading mechanism, why learn it?

 Learned of the operating system should be aware that any processing is carried out through the CPU. Program (translated bytecode files .class) can only be loaded into memory and then processed by the CPU. Bytecode file into memory This process is called class loader.

  Before learning to understand the class loader class loader : a class loader class loader process is complete, the whole is divided into Load, Link, Init. Load (load ) byte code file read process, a preliminary check magic number, constant pool, file length, whether a parent class, and then create an instance of java.lang.Class. Link (Link) This process will check whether such final compliance, the correct type, static variables is reasonable to allocate memory for static variables, set default values. References and check whether the correct class. The final completion of the memory structure and layout.  Init (initialization) assignment, if the process other types of static methods used to load another class immediately. 

  Class loading procedure of FIG.

  

  In the class loading process will load the static code block, performing a static variable assignment statement.

 Summary: My understanding of the class loading mechanism to be here, not part of the book also can understand. (Will come back to read a) I understand that here, we need to know is: during class load, respectively, completed the magic number check, constant pool, parent, check some keywords are correct, the correct type, static variable is correct, to allocate memory, assign a value to initialize static variables.

Part III: memory layout (elsewhere called the memory model)

  Later learned operating system we know: our resources are generally stored on the hard disk, the hard drive for plenty of storage, but slow. Although the fast CPU, but expensive, nor can the data on the CPU, our solution is the concept of buffer. It is to add a layer of buffer. This layer is a buffer memory. In other words, the data we start with the CPU to operate on disk memory (buffer) to put in.

  Mainly devoted to the memory of the operation in the JVM, including memory allocation, allocation management, thus ensuring the normal operation of the JVM.

  Below is a chart book gives memory model, which is one of many I've seen better map

  Speaking for a piece on the map:

  • 1.  堆(Heap):这块是内存中占比最大的,存放着绝大多数的对象的实例。它是OOM的主要发源地。同时它是共享的区域,我们所谓的JVM调优,JVM优化就是优化堆内存。顺便说下这块可以调优的地方,就是根据我们的需要来适当的设置堆内存的初始值和最大值。《码出高效》中有提到:如果初始值和最大值差距很大的话,会出现堆内存持续缩小又扩容的问题,这会带来不必要的服务器压力,所以建议将初始值和最大值设置成一致的。 -Xms:1024M   -Xmx:1024M  前边的是初始值,后边的是最大值。
    • 上图可以看到的是:堆又分为 新生代和老年代,新生代分为伊甸去和两个幸存区(两个幸存区又分别是to和from,他to和from是来回颠倒的),默认比例是eden: to :from = 8:1:1 。这么划分有什么意义:首先创建的对象是要放在eden区的,如果伊甸区达到一定水准:就发生YGC(垃圾回收),回收的才能放在幸存区,这时存放幸存的就叫做from,因为它存放了对象,下次再GC就要将幸存的对象放到另外一个幸存区了,也就是to区(要去的地方,空的地方叫to)。幸存一次就有一个年龄,到达一定年龄就要被安排到养老区了。这个年龄叫做阈值,可以设置。为什么要送到养老区呢:是这样的,这个没有被杀死的是因为一直在被用着,那都杀了好多次了,你看反正杀不死我,我还有用,就把我放到养老区把。 这有引出了养老区,养老区是个比较平静的区域,一直被用的存放在这里。还有特别的对象也放在这里。所谓老年代,就是因为GC不频繁,但是不意味这不发生。
    • 如果觉得我讲的不清楚:看原文
  • 2. 元空间  JDK1.8之前,是固定大小的,叫做永久带,不利于调优被替换掉了。取而代之的是元空间,至于为什么会别替代,类的调用这些信息本来是放在永久带的,如果深度过大,就以为的信息过多,然后会导致OOM错误。此外在永久带的垃圾回收也有很多问题无法解决,然后就废弃掉了,换成了元空间,元空间只直接存放在本地内存中,字符串常量存放在元空间,类的元信息,字段,静态属性,方法,常量都存放在元空间。
  • 3. 虚拟机栈 ,堆管存储,栈管运行。这部分内容是线程私有的,没得优化。会报的错误是stackOverFlow。利用栈这一个特殊的数据结构来完成特殊的任务。每个方法的调用成为一个栈帧,栈帧中存放了一些方法相关的信息。至于都存放了什么信息:
    • 方法调用叫做入栈,方法执行完了叫出栈。
    • 局部变量表又是什么呢:存放这方法参数,以及局部变量。
  •  4.本地方法栈:负责本地方法调用。可以调用别的语言写的方法。不做过多的介绍了
  •  5.程序计数寄存器:就是执行到哪里了
    •   
  • 小总结

~待续

Guess you like

Origin blog.csdn.net/star1210644725/article/details/93225701