About N problems with JVM memory

Source: Internet
Author: User
Tags xms

How is the memory area of the JVM divided?

In the memory partition of the JVM, some areas are thread-private, some belong to the entire JVM process, some of them throw oom exceptions, others do not, and understanding the memory partitioning and characteristics of the JVM is the basis of the problem of locating the memory on the line. So how is the JVM memory area divided?

The first is the program Counter Register, where each thread has its own program counter in the JVM specification. This is a small memory space that stores the JVM instruction address of the Java method that the current thread is executing, which is the line number of the byte code. If the native method is being executed, the counter is empty. This memory area is the only memory area in the Java Virtual Machine specification that does not specify any oom conditions.

Second, theJava VM stack (Java virtal machine stack), also belongs to the thread private area, each thread creates a virtual machine stack at the time of creation, the life cycle is consistent with the thread, and when the thread exits, the thread's virtual machine stack is also recycled. The virtual machine stack to maintain a stack of frames, each method call will be stacked, the JVM on the stack frame operation only out of stack and stack two, the method call at the end of the stack operation.

This area stores the local variable table, the compile time to know the basic types of data, object references, method exports and other information.

Thirdly, the local method stack is similar to the virtual machine stack, where the local method stack is the stack that is used when the local method is called, and each thread has a local method stack (Native).

In the heap , almost all instances of Java objects that are created are allocated directly to the heap. The heap is shared by all threads, and the area on the heap is further divided by the garbage collector, such as the new generation, the old age division. When you start a Java Virtual machine, you can use parameters such as "Xmx" to specify the size of the heap area.

V, method area . The method area, like the heap, is shared by all threads, storing meta (meta) data loaded by the virtual machine, including class information, constants, static variables, and code compiled by the instant compiler. It is important to note that the run-time-constant pool is also in the method area. According to the Java Virtual Machine specification, a OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements. Due to the implementation of the early hotspot JVM, the CG generational collection has been extended to the method area, so many people will call the method area the permanent generation. The permanent generation has been permanently removed from Oracle JDK8 and the metadata area (Metaspace) has been added.

Six, run a constant pool (run-time Constant pools), which is part of the method area, is constrained by the memory of the method area, and throws a OutOfMemoryError exception when the constant pool is no longer able to request memory.
In the class file, there is a constant pool of information, in addition to the description of the class version, method, field, interface, and so on. The first four bytes of each class file are called magic number, and it is used to determine if this is a file that can be accepted by the virtual machine; the next four bytes store the version numbers of the class file. Next to the version number, is the constant pool entry. A constant pool holds two major classes of constants:

    • Literal (Literal), such as text string, final constant value
    • Symbolic references, which hold some constants related to compilation, because Java does not have a connected process like C + +, so the field methods these symbolic references need to be converted at run time to get a true memory entry address.
      A constant pool in a class file, also known as a static constant pool , when the JVM virtual machine completes a class mount operation, loads the static constant pool into memory and stores it in the run-time pool.

VII, Direct memory (direct memory) is not part of the Java specification that belongs to the Java Virtual Runtime data area. The Java NiO can use the native method to allocate memory directly outside the Java heap, using the Directbytebuffer object as a reference to this out-of-heap memory.

The following diagram reflects the running Java process Memory footprint:

What are the areas where oom may occur?

According to Javadoc, oom means that the JVM does not have enough memory, and the garbage collector cannot provide more memory. As you can see from the description, the garbage collector typically tries to reclaim memory before the JVM throws OutOfMemoryError.
From the Java Data area analyzed above, in addition to the program counter does not occur oom, which areas will occur in the case of Oom?

First, heap memory. Insufficient heap memory is one of the most common reasons for sending oom, and if there is no memory in the heap to complete the allocation of object instances, and the heap can no longer be expanded, the OutOfMemoryError exception is thrown, and the error message thrown is " Java.lang.OutOfMemoryError:Java Heap Space ". The current mainstream JVM can control the size of the heap memory through-XMX and-XMS, where the heap of oom may be a memory leak, or the heap size allocation may be unreasonable.

Second, the Java Virtual machine stack and the local method stack, the difference between the two regions is that the virtual machine stack for the virtual machine to execute Java method Services, and the local method stack for the virtual machine to use the native method service, in the memory allocation exception is the same. In the JVM specification, two exceptions are specified for the Java Virtual machine stack: 1. If a thread requests a stack that is larger than the allocated stack size, it throws a stackoverflowerror error, such as a recursive call that does not stop, and 2. If the virtual machine stack can be dynamically expanded, and the extension cannot request enough memory, the OutOfMemoryError error is thrown.

Third, direct memory. Direct memory, although not part of the data area of the virtual runtime, is limited by physical memory since it is memory. NiO introduced in JDK1.4 uses the native library to directly allocate memory on out-of-heap memory, but it also causes oom when there is insufficient direct memory.

Four, the method area. With the introduction of Metaspace metadata area, the Oom error information of the method area becomes "java.lang.OutOfMemoryError:Metaspace". For older versions of Oracle JDK, because of the limited size of the permanent generation, and the JVM's garbage collection for the permanent generation is not positive, if the permanent generation of data, such as String.intern () calls, in the permanent generation occupy too much space resulting in insufficient memory, there will also be oom problems, The corresponding error message is "Java.lang.OutOfMemoryError:PermGen space"

Memory Area whether the thread is private whether oom may occur
Program counter Is Whether
Virtual Machine Stack Is Is
Local method Stack Is Is
Method area Whether Is
Direct Memory Whether Is
Heap Whether Is
What is the structure of the heap memory?

Some tools can be used to understand the contents of the JVM's memory, specific areas of memory, what should be the tool to locate it?

    • Graphical tools. The advantage of graphical tools is intuitive, after connecting to the Java process, you can display the heap memory, the use of out-of-heap memory, similar tools have JCONSOLE,VISUALVM and so on.
    • Command-line tools. Such tools can be queried at run time, including Jstat,jmap, and can be viewed on heap memory, method area, and so on. These tools are also used when locating issues on the line. Jmap can also generate a heap dump file, and if it is on Linux, you can pull the heap dump file locally, use Eclipse mat for analysis, or use JHAP for analysis.

In-depth understanding of memory monitoring and diagnostics is followed. Now look at the next question: What is the structure inside the heap?
From the point of view of the garbage collector, the memory can be divided into the new generation and the old age. The allocation rules for memory depend on the combination of which garbage collector is currently in use, and the configuration of memory-related parameters. In the larger direction, the object is assigned to the new generation of Eden , and the large objects go directly into the old age.

First, the new generation of Eden regions , where objects are prioritized in this region, and the JVM can assign a private cache area to each thread, called Tlab(thread Local Allocation Buffer), Avoiding multi-threaded simultaneous allocation of memory requires the use of mechanisms such as locking to affect allocation speed. The Tlab is allocated on the heap and is located in Eden . The structure of the Tlab is as follows:

1234567891011121314 // ThreadLocalAllocBuffer: a descriptor for thread-local storage used by// the threads for allocation.//            It is thread-private at any time, but maybe multiplexed over//            time across multiple threads. The park()/unpark() pair is//            used to make it avaiable for such multiplexing.class ThreadLocalAllocBuffer: public CHeapObj<mtThread> {  friend class VMStructs;private:  HeapWord* _start;                              // address of TLAB  HeapWord* _top;                                // address after last allocation  HeapWord* _pf_top;                             // allocation prefetch watermark  HeapWord* _end;                                // allocation end (excluding alignment_reserve)  size_t    _desired_size;                       // desired size   (including alignment_reserve)  size_t    _refill_waste_limit;                 // hold onto tlab if free() is larger than this

Essentially, Tlab's management relies on three pointers: Start, end, top. Start and end mark the area managed by the Tlab in Eden, which is not used by other threads to allocate memory, top is the allocation pointer, starts at the start point, and, as memory is allocated, moves slowly toward end, triggering tlab when it hits end Refill. So the structure of the in-memory Eden is roughly:

Second, the Cenozoic survivor region . When the Eden region is out of memory, the minor GC, also known as the Cenozoic GC, is triggered, and objects that survive in the minor GC are copied to the Survivor area. I think the role of the survivor area is to avoid triggering full GC prematurely. If there is no Survivor,eden area each time the minor GC sends the object directly to the old age, the old age will soon be out of memory to trigger the full GC. There are two survivor zones in the Cenozoic, and I think the role of two survivor is to improve performance and avoid the occurrence of memory fragmentation. At any time, there is always a survivor that is empty, and in the event of minor GC, the surviving objects of Eden and another survivor are copied to the empty survivor, thus avoiding the generation of memory fragments. The new generation of memory structure is generally:

Third, the old age . The old age of the long life cycle of objects, usually copied from the survivor area of the object, but when the object is too large to be used in the new generation of continuous memory storage, then this large object will be directly allocated to the old age. In general, ordinary objects are allocated on Tlab, larger objects that are directly allocated to other memory areas on the Eden area, while oversized objects are allocated directly to the old age.

Iv. permanent Generation . As mentioned earlier, there is a concept of the old age in the early-morning hotspot JVM that stores metadata for Java classes, Chang, intern strings, and so on. After JDK8, the old generation is removed, and the concept of metadata area is introduced.

Five,vritual space . As mentioned earlier, you can use XMS and xmx to specify the minimum and maximum space for the heap. If XMS is less than XMX, the size of the heap does not scale directly to the upper limit, but rather leaves a portion waiting for the memory requirements to grow and then allocate to the new generation. Vritual space is this partially reserved area of memory.

So in summary, you can draw the memory structure of the Java heap is basically:

With some parameters, you can specify the size of the heap memory area described above:

    • -XMX value Specifies the maximum heap size
    • -XMS value specifies the initial minimum heap size
    • -xx:newsize = value Specifies the size of the Cenozoic
    • -xx:newratio = value of the old age and the size of the Cenozoic. By default, this ratio is 2, which means that the old age is twice times as large as the Cenozoic. When the old age is too big, full GC time will be very long, the old age is too small, it is easy to trigger the full gc,full GC frequency is too high, this is the effect of this parameter.
    • -xx:survivorration = value. Sets the size ratio of Eden to Srivivor, and if the value is 8, representing a Survivor is Eden's 1/8, which is the entire Cenozoic 1/10.
What are the common performance monitoring and problem locator tools?

In the performance analysis of the system, CPU, memory and IO are the main concerns. Many times the service problem, in these three will appear, such as CPU soared, memory is not enough to occur oom, this time need to use the corresponding tools to monitor performance, to locate the problem.

For CPU monitoring, you can first use the top command for viewing, and here's one for the load using Top view:

The load average represents a system average load of 1 minutes, 5 minutes, and 15 minutes, from which three numbers can be used to determine whether the system load is large or small. When the CPU is completely idle, the average load is 0, and when the CPU workload is saturated, the average load is 1. So the lower the three values of the load average, the smaller the system load, when can you see how heavy the system load is? This article (Understanding Linux CPU Load–when Should you is worried) is very popular. If the computer has only one CPU, the CPU as a single-line bridge, there is only one lane on the bridge, all cars must be passed from this bridge. So

The system load is 0, which means there's no car on the bridge.

System load 0.5 means there's a car on half the bridge section.

System load of 1 means that the road on the bridge has been fully occupied by the car

The system load of 1.7, which means that the car is full on the bridge (100%), while 70% of the car is waiting to pass through the bridge:

From the top command, you can see that the three values of the machine's load average are very low. If these three values are very high, such as exceeding 50% or 60%, it should be noticed. From the time dimension, be wary if you find that the CPU load is slowly rising.

The use of other performance monitoring tools, such as memory and CPU, is shown in a brain graph:

The specific use of the method can be referenced from the first line failure to think about the Java problem positioning ideas.

Reference
    • "In-depth understanding of Java virtual machines"
    • Https://www.cnblogs.com/dreamroute/p/5946272.html
    • https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-2.html#jvms-2.5
    • Https://www.cnblogs.com/Kidezyq/p/8040338.html
    • Https://www.cnblogs.com/baihuitestsoftware/articles/6405580.html
    • Https://www.jianshu.com/p/cd85098cca39
    • Http://www.ruanyifeng.com/blog/2011/07/linux_load_average_explained.html

Original address: http://www.cnblogs.com/QG-whz/p/9636366.html

N questions about JVM memory (RPM)

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.