Analysis of JVM memory structure and 6 regions example very good

Source: Internet
Author: User
Tags xms

Memory, as an important resource in the system, plays a key role in the stable operation and efficient operation of the system, unlike languages like Java and C, which do not require the developer to allocate memory and reclaim memory, but rather the JVM manages the allocation of object memory and the collection of object memory (also known as garbage Collection, GC). This greatly reduces the difficulty of writing the program for developers, but one side effect is that it is difficult to know what the cause is when the system is running with a memory exception thrown by the JVM (such as OutOfMemoryError), and on the other hand, to write high-performance programs, Memory is often required to improve performance, so how to properly use memory and allow the JVM to properly recycle the memory must be mastered, this article will be the main

In fact, for our general understanding of the computer memory, it is the CPU and the computer to deal with the most frequent areas, all the data is first through the hard disk to memory, and then by the CPU to get the data from the memory for processing, and then save the data to memory, by paging or slicing technology to the memory of the data into the hard disk. What about the memory structure of the JVM? The JVM is a platform that runs on the operating system, but runs independently of the OS, and its memory should at least include areas like registers, stacks, and so on.

The JVM divides the data into 6 regions for storage at runtime, not just the well-known heap area, which is illustrated by the 6 regions:

Allocation structure of JVM memory

The following sections describe the work done in each region and what functions it serves.

PC Register (PC register)

The PC register is a small area of memory in which the main function is to record the line number of the bytecode executed by the current thread. The bytecode interpreter works by changing the current thread's program counter to select the next bytecode directive. Any branch, loop, method call, Judgment, exception handling, thread wait and resume thread, recursion, etc. are all done through this counter.

Since Java multithreading is implemented by alternating threads switching and allocating processor time, at any given time, a kernel in the processor executes only one thread of instruction. Therefore, in order for the thread to wait for the end to return to the correct location for execution, each thread will have a separate program counter to record the line number of the current instruction. The counters are independent of each other, and we call this memory "thread-private".

If the method being called is native, no information is stored in the PC register.

L JVM Stack

The JVM stack is thread-private, and each thread creates a JVM stack that is stored in the JVM stack as a variable of the local base type in the current thread (eight basic types defined in Java: Boolean, Char, Byte, short, int, long, float, Double), part of the return result, and the stack Frame, the non-primitive type of object on the JVM stack only holds a point to the heap address, so the basic type of Java variables are value passing, and not the basic type of variable is a reference pass, Sun JDK implementation of the JVM stack space is in the physical Allocated on the memory, not from the heap.

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

When there is not enough space in the JVM stack, a stackoverflowerror error is thrown, and the size of the stack can be specified by-XSS in the sun JDK, such as the following code:

New Thread (New Runnable () {public            void run () {               loop (0);            }                   private void loop (int i) {               if (i!=1000) {                   i++; loop (i);               }               else{                   return;               }            }           

  

When the JVM parameter is set to-xss1k, an error similar to the following will be reported after running:

Exception in Thread "Thread-0" Java.lang.StackOverflowError

Heaps (heap)

Heap is the most familiar area, it is the JVM used to store object instances and array values of the area, you can think of all the Java objects created by the memory of this allocation, the memory of the object in the heap needs to wait for GC to recycle, heap on 32-bit operating system maximum of 2G, There are no restrictions on 64-bit operating systems, and their size is controlled by-XMS and-xmx,-xms the minimum heap memory requested for JVM startup, 1/64 of physical memory by default, but less than 1g,-xmx for the maximum heap memory that the JVM can request, default to 1 of physical memory/ 4, by default when the free heap memory is less than 40%, the JVM will increase the size of the heap to the size specified by-XMX, which can be specified by-xx:minheapfreeratio=, and when the free heap memory is greater than 70%, the JVM will resize the heap to the size specified-XMS , this scale can be specified by-xx:maxheapfreeratio=, but for a running system, to avoid frequent heap size, the values of-XMS and-xmx are usually set to the same, so these two parameters for scaling are usually useless. In fact, the JVM in the allocation of heap memory, use, management, collection, etc. have a more sophisticated design, specifically in the JVM heap memory analysis can be described in detail.

The OutOfMemory error message is thrown when the heap needs to use more memory than its allowed size.

L method Area (Methodarea)

The method area holds information about the class being loaded (name, modifier, and so on), static variables in the class, constants defined as final in the class, field information in the class, 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 and the importance of the method area is visible. Similarly, the method area is shared globally, and it will be GC when the virtual machine is started, and will throw a outofmemory error message when the method area needs to use more memory than it allows.

In the Sun JDK, this area corresponds to the permanetgeneration, also known as the persistent generation, the default is 64M, you can specify its size by-xx:permsize and-xx:maxpermsize.

L Run a constant-rate pool (runtimeconstant)

Similar to the symbol table in c, stored as a fixed constant information in the class, Methods and field reference information, and so on, its space from the method area is allocated. The Chang of a class or interface is assigned when the class file is successfully loaded by the Java virtual machine.

L Local method Stack (Nativemethod Stacks)

The JVM uses the local method stack to support the execution of the native method, which is used to store the state of each native method call.

For example, there is this code:

public class A {public                    static void Main (String[]args) {            String a= "A";           String b= "B";            String ab= "AB";            System.out.println ((a+b) ==ab);       False            System.out.println (("a" + "B") ==ab);   True            final String afinal= "a";            String result=afinal+ "B";            System.out.println (RESULT==AB);      True            String plus=a+ "B";            System.out.println (PLUS==AB);        False              System.out.println (Plus.intern () ==ab);  True     

  

Analysis of the results of the above code execution, can be assisted by javap–verbose A to understand the analysis.

L (a+b) ==ab

A+b is a two-variable addition that needs to run to determine its value, and the JVM adds a new object after the runtime, so the result of A+b==ab is false.

L ("a" + "B") ==ab

"A" + "B" is constant, at compile time the JVM has changed it to "AB" string, and ab= "AB" is also a constant, both in the constant pool is the same address, so ("a" + "B") ==ab is true.

L Result==ab

result=afinal+ "B", Afinal is a final variable, and result is converted to "AB" at compile time, and "AB" is the same address in the constant pool, so result==ab is true.

L Plus=ab

The case for Plus and a+b is the same, so Plus==ab is false.

L Plus.intern () ==ab

The difference here is that the Plus.intern () method is called, and the function of this method is to get the constant pool address of the plus point, so Plus.intern () ==ab is true.

After mastering the mechanism of JVM object memory allocation, let's take a look at how the JVM does automatic object memory recovery, which refers to the heap and method area recycling, and the recovery of several other areas is managed by the JVM simply by life cycle.

Original link: http://blog.csdn.net/zhaozheng7758/article/details/8623562

Analysis of JVM memory structure and 6 regions (RPM) Very good example

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.