JVM run-time data area of the memory overflow exception [A]

Java virtual machine memory division during the execution of the Java program in which it will manage for a number of different data areas.


Under virtual machine specification, Java virtual machine memory management will include the following runtime data area (based on 1.7):
Here Insert Picture Description
each region as detailed in the following figure (based on 1.8):
Here Insert Picture Description

1. Program Counter

The program counter is a small memory space,The main role is to store the byte code line number of execution threads

Because multi-threaded Java Virtual Machine is in turn switched and allocation of processor execution time achieved by a thread at a time, one thread in a processor instruction execution only. Therefore, in order to switch the thread can be restored to the correct location of execution, each thread requires a separate program counter.

This memory area is the only Java virtual machine specification does not specify any area OOM situation

2. VM stack

Virtual machine thread stack is private, for each thread, JVM thread will be created when creating a part of the thread's stack. VM stack is describedMemory model method of execution: Stack frames are used to support virtual machine data structures and methods for performing the method call, and each method will be executed on the thread stack to create a stack frame for storing local variable table, the operand stack, dynamic linking method export and other information.Each method performed from the beginning to the end of execution on a stack frame corresponds to push the stack in a virtual machine.

Corresponding to a thread stack, the stack frame corresponding method

In the event thread, top of the stack stack frame called the current stack frame, the method being performed is called the current method. In the run-time execution engine, all instructions operate only on the current stack frame. VM stack by push and stack mode, the method corresponding to each active stack frame arithmetic processing, the method is successfully completed, it will jump to another stack frame. If this method is called, it will jump to call this method the stack frame, which is for export; if there is an exception, an exception will be back, return address determined by exception handling tables.

Local variable table stored for all the basic data types, object referenceWherein the long and double-length 64-bit data occupies two local variable space, occupies only one other types.Desired local variable table memory space to complete the assignment at compile timeWhen this method requires space for local variables allocated in the frame much enters a method is well defined, the method does not change during operation the size of the local variables.

Operand stack is a stack structure, when the stack frame is created by the JVM to perform a method, creates an operand stack in the stack frame of the method, the method ensures that instructions can be done.

Dynamic Link: Each stack frame contains a constant pool reference to the current method, the purpose is to support the process of dynamic linking method calls.

The method of the return address: There are two exits from performing the methods: a normal exit (return bytecode instruction encountered, such as RETURN, IRETURN, ARETURN); 2 aborted... Either quit, should return to position method is called. The next program counter 3 after executing the call; 1 returns the value onto the stack frame Über; 2 exception stack frame information that can be processed thrown: Method exit process is equivalent to the pop-up current stack frame, there are three ways to exit... instruction.

Virtual machine specification, this provides two regions of memory exception: if a thread requests stack depth greater than the depth allowed by the virtual machine, throw exception StackOverflowError; 2 can not apply to a virtual machine if the stack dynamically expanding. enough memory, will throw OOM.

3. The native method stacks

Now that java has been very mature, native methods use less than basic, so HotSpot simply implement the native method stacks and stack virtual machines combined.

4. heap

Heap isThe biggest piece of memory managed by the Java Virtual Machine. It is allThreads share, Created when the virtual machine starts.Its sole purpose is to store objects, So almost all object instances in the heap allocation of memory (with the development of JIT compiler technology matures and escape analysis, allocation on the stack, scalar replacement optimization techniques will cause the object may not be allocated on the heap), Java Virtual Machine Specification description: all object instances and arrays to be allocated on the heap.

Heap garbage collector (Recommended reading garbage collector of the JVM [three] main area) management: 1. From the perspective of memory recovery of view, the current collector is basically using generational collection algorithm (Recommended reading JVM's garbage collection algorithms and strategies [two] ), so the stack can also be subdivided into the new generation and the old era, then more carefully into Eden area, from Survivor region, to Survivor region; 2 from the point of view of memory allocation, the stack may be divided into a plurality of threads assigned private buffers (the Local the thread buffer allocation,. Tlab ): Object preferentially allocated in the new generation area Eden, multi-threaded environment to avoid locking mechanisms influence object dispensing speed, the JVM may be assigned a private cache area for each thread ( Tlab ), this spaceWhen allocating thread is exclusive when using a shared thread,and soHeap is not shared by all threads. In essence, TLAB management relies on three pointers: start, end, top, start and end are marked area of ​​the TLAB management of Eden, this area will not be used when other threads allocating memory, top pointer is assigned, when the start point to the starting position, with the memory allocation, slowly approaching the end, when the trigger TLAB refill equal to end. In any case, the heap objects are stored.

TLAB structure:
Here Insert Picture Description
the structure of the Eden area:
Here Insert Picture Description
heap memory structures:
Here Insert Picture Description

Heap Walker tool memory: (JConsole and VisualVm- I use is this, you can install the plug-Visual GC, navigation bar Tools -> Add-Ins inside)
Here Insert Picture Description

Extending knowledge: top view cpu command usage, load the representative average 1 minute, 5 minutes, 15 minutes system load average, these three numbers, based on the system load is large or small. When the CPU is completely idle, the average load is 0; when the CPU workload saturated average load 1.
Here Insert Picture Description

Under the Java Virtual Machine specification, Java heap may be in discontinuous physical memory space, as long as the continuous logically, like disk space. When implemented, HotSpot virtual machine is implemented in extensible (by the minimum heap space -Xms, the maximum heap space -Xmx, the size of the new generation control parameters -Xmn). If there is no space to complete the assignment object instance, and can no longer expand the heap, it will OOM.

The method area

The method area and the Java heap is the same as all the threads share the memory area, which is used to storeClass information has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other metadata, Every time a class is loaded for the first time, he will be placed in the metadata area method.

The method area is the runtime data area from the virtual machine specification defines the management of the most liberal, Java virtual machine specification does not specify how to implement the method area, and there is no clear position on the method area requirements.In HopSpot (JDK1.8 before), but the method area separate logical regions, and does not physically exist independently of the stack, but is located the permanent generation. So this time zone method also can be recycled garbage.

HotSpot virtual machine design team because the GC generational collection extends to the method area, such HotSpot garbage collector heap management can be like this as part of memory management, so many people to the method area called "perpetual generations." But to achieve permanent-generation method area can easily lead to memory overflow. In JDK1.7, HotSpot originally in a string constant pool area is moved to the heap method. JDK1.8 is permanently substituting element space (in local memory) Alternatively, metadata information storage class (field method), static properties, constant.

Nature and methods of space-like region yuan, is the realization of JVM specification method area, but the space between the yuan and the biggest difference is that the method area: Yuan is not space in a virtual machine, but the use of local memory. Therefore, by default, the size of the element space only by the local memory limit.

This part of the same and do not require continuous stack memory and can select a fixed size or may be expanded, may choose not to implement garbage collection. Memory recovery target zone method is mainly for recycling and unloading constant pool of the class, but the class unloading conditions are harsh, but this part of the region memory recall is necessary.

JVM corresponding parameter adjustment:
Here Insert Picture Description
According to the Java Virtual Machine specification, when the method of memory allocation area can not meet demand, would OOM.

6. runtime constant pool

Runtime constant pool is part of the zone method. Class file versions in addition to the classes, fields, methods, and interface description information, as well as a constant pool information. Class first four bytes of each file is called Magic Number, its purpose is to determine whether it is a virtual machine can be accepted file; then four bytes are stored in the file version number of Class; next version is the constant pool entry number. For various constant pool storing literal compiler generated (such as text strings, final constant value) and a reference symbol (a symbol reference symbols of these fields will be converted at runtime to obtain real memory address entry), this part will be stored into the operation region method after the class loading time constant pool.

Java Virtual Machine Specification no runtime constant pool any details of the requirements, but the file format Class constant pool there are strict requirements. In general, in addition to saving a file Symbol Description Class references will be translated out of the direct reference run is also stored in the constant pool.

Runtime constant pool for another important feature of Class includes file constant pool is dynamic. Not necessarily to produce a constant at compile time, runtime can also be placed in constant (intern () method of the String class) pool.

7. Direct Memory

Memory is not a direct part of the JVM run-time data area, nor is it Java virtual machine memory area defined in the specification, but also OOM.

NIO JDK1.4 newly added category, based on the introduction of a channel with buffer I / O mode, the Native libraries can be directly allocated heap memory outside, and by storing this object as DirectByteBuffer Java heap memory references operation. Copy data back and forth to avoid the heap and stack in Java Native, thereby improving performance.

Here Insert Picture Description

Direct Memory is not limited by the size of the Java heap, but will be limited physical memory, it may also occur when OOM dynamic expansion.

to sum up:
1. In addition to the program counter, all other memory areas will occur OOM;
memory usage of running processes 2.java
Here Insert Picture Description

Published 11 original articles · won praise 0 · Views 611

Guess you like

Origin blog.csdn.net/fei1234456/article/details/105037544