Java JVM: memory area and memory overflow exception (1)

JDK: Java programming language, Java virtual machine, Java class library
JRE: The standard environment for running Java programs
HotSpot: The default Java virtual machine in Sun/OracleJDK and OpenJDK

1. Runtime data area

insert image description here

1.1 Thread private area

  • program counter
    • The line number indicator of the bytecode being executed by the current thread
    • Select the next bytecode instruction to be executed by changing the value of this counter
    • Each thread has an independent program counter, which does not affect each other and is stored independently, so the thread is private
    • The only zone that doesn't specify any OOM situations
  • virtual machine stack
    • Life cycle is the same as thread
    • When each method is executed, the Java virtual machine will synchronously create a stack frame for storing information such as local variable table, operand stack, dynamic connection, method exit, etc.
      • The local variable table contains various basic data types and object reference types known to the compiler
      • The storage space of the local variable table is represented by local variable slots, and the 64-bit Long and Double types occupy two variable slots
    • Each method is called until the process of execution is completed, corresponding to a stack frame in the virtual machine stack from the stack to the stack road process
    • abnormal
      • The stack depth requested by the thread is greater than the depth allowed by the virtual machine: StackOverflowError exception
      • When dynamic expansion is allowed, it can no longer be expanded: OutOfMemoryError exception
  • native method stack
    • Very similar to the virtual machine stack, the virtual machine stack serves for the virtual machine to execute Java methods, and the local method stack serves for the virtual machine to use the local (Native) method
    • The HotSpot virtual machine directly combines the local method stack and the virtual machine stack into one

1.2 Thread shared area

  • heap
    • Created when the virtual machine starts to store object instances, all object instances and arrays should be allocated on the heap
    • The Java heap is an area of ​​memory managed by the garbage collector
    • The Java heap can be fixed in size or expandable. The mainstream Java virtual machines are all implemented in accordance with scalability (parameters -Xmx and -Xms settings)
  • method area
    • Store data such as type information, constants, static variables, and code caches compiled by the compiler that have been loaded by the virtual machine
    • runtime constant pool
      • The Class file contains description information such as the version, fields, methods, and interfaces of the class, and there is also a constant pool table
      • Used to store various literals and symbol references generated by the compiler, and stored in the runtime constant pool of the method area after class loading
      • dynamic
      • The commonly used String.intern() method is put into the runtime constant pool

1.3 Direct memory

  • It is the memory outside the Java heap. The direct memory is not in the Java heap. Writing out of the Java heap memory needs to be copied to the native heap.
  • Virtual Memory
    • When the memory is used up, divide a part of the hard disk to act as memory

Two, HotSpot virtual machine object exploration

2.1 Object Creation

  • When the virtual machine encounters the new instruction, it will first check whether the parameter of this instruction can locate a symbol reference of a class in the constant pool, and check whether the class represented by the symbol reference has been loaded, parsed and initialized, if not, it must Execute the class loading process
  • After the check is passed, allocate memory for the new object, and divide a memory block of a certain size from the Java heap
    • Pointer collision: The used memory and the free memory are placed on both sides respectively, and the pointer is placed in the middle as the dividing point. To allocate memory is to move the pointer to the free direction for a certain distance
    • Free list: The used and free ones are interleaved, so it is necessary to maintain the free list. When allocating, find a large enough space from the list to allocate to the object
    • Concurrency
      • Use CAS with failed retries to ensure atomicity of update operations
      • Local thread allocation cache: each thread pre-allocates a small chunk of memory in the Java heap
    • Then execute the () method, which is the construction method, and initialize the object according to the programmer's wishes

2.2 Object memory layout

  • object header
    • Used to store the runtime data of the object itself
      • Such as: hash code, GC generational age, lock status flag, lock held by thread, biased thread ID, biased timestamp, etc.
  • type pointer
    • The Java virtual machine uses this pointer to determine that the object is an instance of that class
      • If the object is a Java array, the object header must also have a piece of data for recording the length of the array
  • instance data
    • The contents of various types of fields defined, whether inherited from the parent class or fields defined in the subclass, must be recorded
      • Fields of the same width are always allocated together for storage
  • Align padding
    • The starting address of the object must be an integer multiple of 8 bytes

2.3 Access positioning of objects

  • The Java program manipulates specific objects on the heap through the reference data on the stack
  • interview method
    • handle access
      • A piece of memory is allocated in the Java heap to be used as a handle pool, and the reference stores the address of the object handle
    • direct pointer access
      • The reference stores directly the object address

insert image description here
insert image description here

  • Respective advantages: handle access does not need to change the reference itself when the object is moved, direct pointer access is fast, and saves the time spent on pointer positioning
  • HotSpot mainly uses direct pointer access. From the perspective of overall software development, handle access is also very common.

3. OOM exception

3.1 Java heap overflow

  • Heap min: -Xms
  • Maximum heap size: -Xmx
  • The same parameter settings can avoid automatic expansion
  • Memory leak: The heap memory that has been dynamically allocated is not released or cannot be released by the program for some reason, resulting in a waste of system memory, resulting in program slowdown and system crash
  • specific situation
    • Static collection class: long-lived objects hold references to short-lived objects, which cannot be recycled
    • Various connections: database connection without close, network connection and IO connection, etc.
    • Unreasonable variable scope: the scope of the variable is greater than the scope of use, and it is not set to null in time
    • The inner class holds the outer class: the outer class object returns the inner class instance object, the inner class is referenced for a long time, and the outer class cannot be garbage collected
    • Change hashes, expired references, cache leaks, listeners and callbacks
  • avoid
    • Use less enumerations, try to use static inner classes instead of inner classes, try to use lightweight data structures, use static keywords carefully, and use singleton patterns carefully
  • Memory overflow: not enough memory, exceeding the specified size
    • Modify the JVM startup parameters to directly increase the virtual machine memory

3.2 Virtual machine stack and native method stack overflow

  • The stack capacity is set using the -Xss parameter
  • If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, or the new stack frame memory cannot be allocated, a StackOverflowError exception will be thrown
  • If the stack memory of the virtual machine runs dynamic expansion, when the expansion stack capacity cannot apply for enough memory, an OutOfMemoryError exception will be thrown
  • The maximum memory limit of a single process in Windows is 2GB

3.3 Method area and runtime constant pool overflow

  • The intern() method will copy the first encountered string instance to the string constant pool in the method area for storage
  • -XX:MaxMetaspaceSize: Set the maximum value of metaspace, default -1 (no limit)
  • -XX:MetaspaceSize: Specifies the initial space size of the metaspace
  • -XX:MinMetaspaceFreeRatio: Controls the minimum metaspace free ratio after garbage collection

3.4 Local direct memory overflow

  • The capacity size parameter of direct memory
    • -XX:MaxDirectMemorySize
    • If not specified, the default is consistent with the maximum value of the Java heap (specified by -Xmx)

Guess you like

Origin blog.csdn.net/baidu_40468340/article/details/128544862