JVM Learning Notes: Java Runtime data regions

Source: Internet
Author: User
Tags xms

In the process of executing Java programs, the JVM uses a variety of data regions that have their own purpose, creation, and destruction times. According to the Java Virtual Machine specification, the JVM includes the following run-time data regions, as shown in:

Where the red part is thread-private, each thread has its own copy. The green part is shared by each thread.


1.PC Register (the PC Register)

(1) Each Java thread has a PC register.

(2) The PC Register is the JVM instruction that is used to store the next execution of each thread, and if the method is native, no information is stored in the PC register.

(3) This memory area is the only area in the JVM spec that does not specify any outofmemoryerror conditions.


2.JVM Stack (Java Virtual machine Stacks)

(1) The JVM stack is thread-private, and each thread creates a JVM stack at the same time as the process PC register, and the JVM stack has the same life cycle as the thread.

(2) variables stored in the JVM stack as local primitives in the current thread (eight basic types defined in Java: Boolean, Char, Byte, short, int, long, float, double), partial return result, and Stack Frame, Objects of non-basic types hold only one address on the heap to the JVM stack.

(3) in the JVM spec, there are two exceptions to this area: if the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, and if the JVM stack can be dynamically extended (JVM spec allows fixed-length JVMs), The OutOfMemoryError exception is thrown when the extension cannot request enough memory.

(4) because the JVM stack is thread-private, it is highly efficient in memory allocation and is automatically reclaimed when the thread finishes running.


3. Local methods Stack (Native method Stacks)

(1) The local method stack is similar to the JVM stack, except that the JVM stack runs the JVM primitive service for the virtual machine, and the local method stack is the native method service used for the virtual machine. Its implementation of the language, mode and structure is not mandatory, and even some virtual machines (such as Sun hotspot virtual machine) directly into the local method stack and the JVM stack.

(2) Like the JVM stack, this area also throws Stackoverflowerror and OutOfMemoryError exceptions.


4. Method area

(1) aliases are called non-heap (not heaps).

(2) The method area holds information about the class being loaded (name, modifier, etc.), the static variable in the class, the constants defined in the class as the final type, the field information in the class, the method information in the class, and when the developer passes through the GetName in the class object in the program, Isinterface and other methods to obtain information, these data are derived from the method area.

(3) The method area is shared globally, and under certain conditions it is also GC-outofmemory error message is thrown when the method region needs to use more memory than it allows.

(4) In the Sun JDK this area corresponds to the Permanet Generation, also known as the permanent generation, the default is 64M, you can specify its size by -xx:permsize and -xx:maxpermsize .


5. Run a constant pool (runtime Constant)

(1) Similar to the symbol table in c, stored as a fixed constant information in the class, Methods and field reference information, etc., its space from the method area is allocated.

(2) class file in addition to the class version, fields, methods, interfaces and other information such as description, there is also a message is a constant table (Constant_pool table), for the compilation of the known constants, which will be loaded in the class load into the method area (permanent generation) storage. However, the Java language does not require constants to enter the method area constant pool only if the content of the const table of the class is pre-compiled, and the new content can be put into Chang (the most typical String.intern () method) during the run time.

(3) The running constant pool is part of the method area and is naturally constrained by the memory of the method area, which throws a OutOfMemoryError exception when the const pool cannot request to memory.

6.Java Heap (Java heap)

The Java heap is shared by all threads and is created when the virtual machine is started. It is the area where the JVM stores object instances and array values, and most of the object instances are allocated here. After the advent of escape analysis and scalar substitution optimization techniques, not all object instances are allocated here, but we can roughly assume that all the memory of all objects created through new in Java is allocated here. The memory of the objects in the heap needs to wait for the GC to recycle.

The size is controlled by-XMS and-xmx,-xms the minimum heap memory requested when the JVM starts (default is 1/64 of physical memory but less than 1G), and-xmx is the maximum heap memory that the JVM can request (default is 1/4 of physical memory). By default, when the free heap memory is less than 40%, the JVM increases the size of the heap to the size specified by-XMX, which can be specified by-xx:minheapfreeratio=. By default, when the free heap memory is greater than 70%, the JVM will resize the heap to the size specified by-XMS, which can be specified by-xx:maxheapfreeratio=. But for a running system, to avoid frequent heap size adjustments, the values of-XMS and-xmx are usually set to the same, so these two parameters for scaling are usually useless.

The JVM divides the heap into new Generation and old Generation (or tenured Generation) two blocks for management:

(1) New Generation

Also known as the Cenozoic, new objects in the program are assigned to the Cenozoic, and the Cenozoic is made up of Eden Space and two blocks of survivor space, which can be specified by the -xmn parameter. The garbage collection action that occurs in the Cenozoic is called the minor GC.

(2) Old Generation

Also known as the Old Generation (old age), used to store the program after a few garbage collection survived the object, such as cached objects, such as the memory of the old generation of the size of the -xmx specified by the size minus-xmn specified size . The garbage collection action that occurs in the old age becomes major gc/full GC.

(3) Memory allocation policy

A. The object takes precedence over Eden allocation. B. Large objects go directly into the old age. C. The long-term survival of the target will enter the old age.


Explanation of the heap:

1) The heap is shared by all threads in the JVM, so the allocation of the object memory on it requires locking, which also results in a larger cost to the new object.

2) for the above reasons, the Sun Hotspot JVM allocates a separate space for the created thread in order to increase the efficiency of object memory allocation, which is also known as Tlab (thread Local Allocation Buffer). The size is calculated by the JVM according to the running situation, the allocation of objects on the Tlab does not need to lock, so the JVM will allocate memory to the thread's objects as much as possible on the Tlab allocation, in this case the JVM allocates object memory performance and C is basically as efficient, However, if the object is too large, the heap space allocation is still used directly.

3) Tlab only works on the new generation of Eden Space, so when writing a Java program, it is usually more efficient to allocate more small objects than large objects, but this approach also brings two problems, one is the waste of space, and the other is that the object memory is still not as efficient as stack. It also increases the consumption of resources at the time of collection, and can be used to view the usage of Tlab by adding -xx:+printtlab to the startup parameters.

Resources

Deep Java Virtual machines: VM Advanced features and best practices

JVM memory Management: Deep Java memory area and Oom http://icyfenix.iteye.com/blog/802573

Http://itindex.net/detail/48698-jvm-%E5%86%85%E5%AD%98

Deep understanding of the JVM-JVM memory model http://yhjhappy234.blog.163.com/blog/static/316328322011101723933875/?suggestedreading& Wumii

"The Java®virtual Machine specification" http://docs.oracle.com/javase/specs/jvms/se8/html/index.html

JVM Learning Notes: Java Runtime data regions

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.