Deep understanding of Java Virtual Machine Fu Yun source code download

Source: Internet
Author: User

上周末搬家后,家里的宽带一直没弄好,跟电信客服反映了N遍了终于约了个师傅明天早上来迁移宽带,可以结束一个多星期没网的痛苦日子了。这段时间也是各种忙,都一个星期没更新博客了,再不写之前那种状态和激情都要慢慢褪去了,总觉得心里慌的一逼,这怎么行呢?!趁明天周末,在公司电脑上记录下这周的一些学习内容。近期在看一本很经典的java书籍:《深入理解java虚拟机 第二版》,几年前也翻过,但那时候功力不够,不太能看懂就没看了。现在回过头来看,发现确实写的很好,很多知识点都能理解了,而且讲的也很有深度,收获颇多。后期计划按照这本书的内容写出一系列博客,来深入学习和复习下java虚拟机相关的知识。

Fu Yun Source code download
Address one: "hubawl.com" Fox PA Source Forum
Address two: "Bbscherry.com"

   First, JVM memory model Overview The JVM memory model is actually quite simple, here are 2 knowledge points: 1, composed: Java heap, Java stack (that is, virtual machine stack), local method stack, method area and program counter. 2, whether to share: where the method and heap areas are thread-shared, virtual machine stacks, local method stacks and program counters are thread-private, also known as thread-isolated, each region stores different content.   These 2 points of knowledge must be kept in mind as a basis for mastering the JVM's memory model. The program counter in the JVM of the program counter is a very small area of memory, but this area of memory is quite interesting. The main features are 3:1, Storage content: For the Java common method (that is, no native keyword modification method), stored is the execution of the current instruction in the address, and for the native method, here is empty (undefined), why?          The memory address of the JVM virtual machine may have been exceeded because the local method was called. 2. Thread-Private: Why is the program counter private to the thread?          According to the storage content or understanding, if the thread is shared, that multiple threads execution, do not know when the front-end of the execution of the address is which, some threads are fast, some threads slow, fast execution to enter the next, and so slow the execution of the thread back to find their address has changed, not chaos?   3. Is the only area in the JVM that does not report a memory overflow (outofmemoryerror). Third, virtual machine stack virtual machine stack is mainly stored in a stack frame, each stack frame is stored in the local variable table, the operation of the stack, dynamic link and method export information. The local variables table stores some local variables defined in the method, object references, parameters, and the return address of the method. The amount of space occupied by a local variable table can be determined at compile time, and when the method is running, it does not change the spatial size of the local variable table, which is well understood in the context of local variable table storage. The operand stack can be understood as the data into the current operation of the stack, for 64-bit length long and double type, each operand occupies 2 word width (slot), other types of operands occupy a word width (slot). Each method call creates a stack frame, which corresponds to the process of a stack frame from the stack to the stack in the virtual machine stack. About the contents of the stack frame can be referred to a netizen wrote a blog: 41924283, speak very good and detailed.       Here is a picture of the stack frame, see at a glance. There are 2 scenarios for memory overflow on a virtual machine stack: 1. The stack depth of the thread request exceeds the allowed depth of the virtual machine, which throwsOut of Stackoverflowerror, so when we see this exception in the code, it should be thought that there might be a problem with the virtual machine stack.   2, if the virtual machine stack can be dynamically extended (most of the current JVM can be dynamically extended, but the JVM also allows a fixed-length virtual machine stack), when the extension cannot request enough memory, will throw OutOfMemoryError exception. Four, the local method stack this knowledge point is relatively simple, the local method stack and the virtual machine stack function similar, but is to call the native method for the JVM service, and the JVM to the local method used by the language (such as Java calls the C language implementation of the function, you need to define the native method to implement), Usage and data structures are not mandatory, so different virtual machines can be implemented freely. And the hotspot virtual machine directly merge the local method stack with the virtual machine stack.   Similar to the virtual machine stack, the local method stack throws Stackoverflowerror and OutOfMemoryError. The method area method area is a more important area, and the Java Virtual Machine specification describes the method area as a logical part of the heap, but in order to correspond to the heap, it is also called non-heap (non-heap area). The main storage is static variables, constants (including run-time constant), class loading information and Java compiled code. This part of the space does not need to be contiguous, can choose a fixed size and extensible, usually in this part is not GC, because GC recycles are some static variables, constants and class loading information, these objects are often unsatisfactory collection, so you can choose not to implement garbage collection.   This area is also known as a persistent generation, and when this block of memory is low, outofmemoryerror exceptions are reported. The heap area Java heap is the fattest chunk of the JVM's memory because it stores both the object's instance and the array object. This area is thread-shared, and is created when the JVM is started, think that if such a large space is thread-private, then the memory should not be blown out? According to the Java Virtual Machine specification, the contents of the heap can be physically discontinuous, as long as it is logically contiguous, it can be fixed or extensible, and usually extensible, and the memory parameters-xms and-XMX are used to regulate the heap size. The Java heap is divided into the new generation and the old age according to the life cycle. The Cenozoic can be subdivided into Eden and survivor areas, while survivor can be subdivided into Survivor1 and Survivor2, which usually use only one piece and the other as the GC to preserve the surviving object. Most of the new objects are stored in the Eden area, if they are large objects, such as a large array or a list ofLike, you can use the JVM parameter-xx:pretenuresizethreshold to deposit more than the specified size of the object directly into the old age, it should be noted that the writing process should try to avoid the death of the big object into the old age, because compared to the younger generation of GC, the old time GC cost more. 8:1:1 the default size ratio of Eden and Survivor, the default GC algorithm for the new generation is the copy algorithm. The default GC algorithm for the old age is the labeling collation method. The 2 GC algorithms are explained in the next blog post.

A OutOfMemoryError exception is thrown when there is not enough memory in the heap. For a memory model of the heap area, refer to the following image:

Deep understanding of Java Virtual Machine Fu Yun source code download

Related Article

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.