This article takes you to understand the memory management of the jvm virtual machine

Runtime data area

  • Program counter (thread private)
    • A small memory space can be regarded as a line number indicator of the bytecode executed by the current thread. If the thread is executing a java method, the address of the bytecode instruction of the virtual machine being executed is recorded; if the native method is executed, the counter value is empty. Note: This memory area is the only one that does not specify any OutOfMemoryError in the java virtual machine specification
  • java virtual machine stack (thread private)
    • The virtual machine stack describes the memory model of java method execution: each method will create a stack frame (stored frame) used to store local variable table, operand stack, dynamic link, method exit and other information during execution.
    • The local variable table stores various basic data types, object references, and returnAddress types (pointing to the address of a bytecode instruction) that are known at compile time. Note: Among them, 64-bit long and double type data will occupy 2 local variables Space (slot), the remaining data only occupy one.
    • Two exceptions in the stack area: StackOverflowError and OutOfMemoryError exceptions
  • Local method stack (thread private)
    • The local method stack is the native method service used by the virtual machine
  • java heap
    • The heap is the largest piece of memory managed by the virtual machine, and the java heap is a memory area shared by all threads, created when the virtual machine starts
    • Java heap breakdown: new generation and old generation. Be more careful about Eden space, from survivor space, To survivor space, etc. From the perspective of memory allocation, multiple thread private allocation buffers (Thread Local Allocation Buffer, TLAB) may be divided in the java heap shared by threads
    • -Xmx and -Xms set whether the virtual machine heap memory can be expanded
  • Method area (alias Non-Heap non-heap)
    • It is used to store data tables such as class information, constants, static variables, and code compiled by the compiler in time by the virtual machine.
    • The runtime constant pool is part of the method area. In addition to the class version, fields, methods, interfaces, and other description information in the class file, there is also a constant pool, which is used to store various literals and symbol references generated by the compiler. This part will be entered after the class is loaded. Stored in the runtime constant pool of the method area.
  • Direct memory
    • In jdk1.4, the new nio class was added, and an i / o method based on channels and buffers was introduced. It can use the native function library to directly allocate off-heap memory, and then use a directByteBuffer object stored in the java heap as this Block memory reference operation. This can significantly improve performance in some scenarios because it avoids copying data back and forth between the java heap and the native heap.
    • The local direct memory allocation is not limited by the java heap size, but if you ignore the direct memory when setting parameter information such as -Xmx, the total memory may be greater than the physical memory limit, resulting in an OutOfMemoryError exception during dynamic expansion.

Several common OutOfMemoryError

  • java heap overflow
    • java.lang.OutOfMemoryError : java heap space
    • When a heap overflow occurs, the general method is to analyze the dump dump snapshot from the dump by using a memory image analysis tool (such as eclipse memory analyzer). The first thing to know is whether it is a memory leak or memory overflow
  • Virtual machine stack and native method stack overflow
    • The stack capacity is only set by the -Xss parameter
    • Regarding the virtual machine stack and the local method stack, two exceptions are described in the java virtual machine specification:
      • If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, StackOverflowerror will be thrown
      • If the virtual machine cannot apply for enough space when expanding the stack, an OutOfMemoryError is thrown
  • Method area and runtime constant pool overflow
    • VM Args:-XX:PermSize = 10M -XX:MaxPermSize=10M
    • java.lang.OutOfMemoryError : PermGen space
  • Local direct memory overflow
    • DirectMemory capacity can be specified by -XX: MaxDirectMemorySize, if not specified, the default is the same as the maximum value of java heap
    • An obvious feature of memory overflow caused by directMemory is that no obvious abnormalities will be seen in the Heap Dump file. If the dump file is small after the OOM is found, and the program uses NIO directly or indirectly, you can consider whether this is the case. Aspect problem

Memory allocation and recycling strategy

Objects are allocated first in Eden

In most cases, objects are allocated in the new generation Eden. When there is not enough space for allocation in the Eden area, the virtual machine will initiate a Minor GC.

Large objects go directly into the old generation

The so-called large object refers to-java objects that require a large amount of continuous memory space (the most typical large objects are those long strings and arrays). The virtual machine provides a -XX: PretenureSizeThreshold parameter, so that objects larger than this setting value are directly allocated in the old generation. The purpose of this is to avoid a lot of memory copying between the Eden area and the two survivor areas.

Long-lived objects will enter the old age

Objects born in Eden, which exist after the first Minor GC and can be accommodated by survivor, will be moved to survivor with age set to 1. Each time, the age +1 until the threshold is reached (default 15, can be set by -XX: MaxTenuringThreshold)

Dynamic age judgment

If the sum of the size of all objects of the same age in the survivor space is greater than half of the survivor space, objects older than or equal to the age directly enter the old generation without waiting for the age to reach the threshold.

Garbage collection algorithm

  • Mark-sweep algorithm
  • Replication algorithm
  • Mark-sort algorithm
  • Generational collection algorithm

Virtual machine performance monitoring and troubleshooting tool

jdk command line tool
  • jps: virtual machine process status tool
    • You can list the running virtual machine processes, and display the name of the virtual machine execution main class (Main Class, the class where the main () function is located) and the local virtual machine unique id of these processes (Local Virtual Machine Identifier, LVMID)
    • -q: output only LVMID, omitting the name of the main class
    • -m: output the parameters passed to the main () function of the main class when the virtual machine process starts
    • -l: output the full name of the main class, if the process executes a jar package, output the path of the jar
    • -v: output jvm parameters when the virtual machine starts
  • jstat: virtual machine statistics monitoring tool
    • JVM Statistics Monitoring Tool is a command-line tool used to monitor various operating status information of virtual machines. It can display operating data such as class reprint, memory, garbage collection, jit compilation, etc. in local or remote virtual machine processes. The preferred tool for locating virtual machine performance problems during runtime
    • -gc: monitor the java heap, including the Eden area, two survivor areas, the capacity of the old generation, permanent generation, etc., the used space, the total amount of gc time and other information
    • -class: monitor class loading, unloading quantity, total space, and time spent in class loading
    • -gcutil: The monitoring content is basically the same as -gc, but the output mainly focuses on the percentage of used space in the total space
  • jinfo: java configuration information tool
    • The role of Configuration Info for Java is to view and adjust various parameters of the virtual machine in real time.
  • jmap: java memory mapping tool
    • The Memory Map for Java command is used to generate heap dump snapshots (commonly known as heapdump or dump files).
    • -dump: generate java heap dump snapshot
      • -dump: [live,] format = b, file = live subparameter indicates whether only the live objects are dumped
    • -heap: displays detailed information about the java heap, such as which collector to use, parameter configuration, generation status, etc.
    • -histo: display statistics of objects in the heap, including class, number of instances, and total capacity
    • -F: When the virtual machine process does not respond to the -dump option, you can use this option to force a dump snapshot.
  • jhat: virtual machine heap dump snapshot analysis tool
    • Sun JDK provides the jhat (JVM Heap Analysis Tool) command used with jmap to analyze the generated heap dump snapshot. A small HTTP / HTML server is built in jhat. After generating the analysis result of the dump file, you can view it in the browser.
    • Note: generally do not use this command to analyze dump files on the production server
      • Analysis tools are a time-consuming and hardware-consuming process
      • jhat's analysis function is relatively simple
  • jstack: java stack trace tool
    • The Stack Trace for Java command is used to generate a thread snapshot of the virtual machine at the current moment (generally called threaddump or Javacore file)
    • -l: In addition to the stack, display additional information about the lock
    • -m: If you call a local method, you can display the C / C ++ stack
    • -F: When the normally output request is not responded, force the output thread stack
Published 8 original articles · Like1 · Visits 263

Guess you like

Origin blog.csdn.net/qq_40635011/article/details/105413114