JVM memory model

The memory managed by the Java virtual machine consists of the following seven runtime data areas:

1. Program counter(Program Counter Register)

  • A small memory space, yesAs a line number indicator of the bytecode being executed by the current thread
  • Thread privateThe memory of
  • It is worth noting that in the Java Virtual Machine Specification,The onlyaNo OutOfMemoryError cases are specifiedArea!!

2. Java virtual machine stack(VM Stack)

  • A thread-memory model for Java method execution
  • Execute Java methods for the virtual machine (i.eThe bytecode) service
  • Thread privateThe memory of
  • itsThe life cycle is the same as that of a thread
  • The execution of each Java method corresponds to the loading and unloading of a stack frame
  • Two types of exceptions:
    • ifThe stack depth of thread requests is greater than the depth allowed by the virtual machineThat will be thrownStackOverflowErrorabnormal
    • ifThe JVM stack size can be dynamically expandedwhenUnable to allocate enough memory for stack expansionIs thrownOutOfMemoryErrorabnormal

3. Local method stack(Native Method Stacks)

  • The difference in“Java Virtual Machine Stack”Local method stackOnly for theNative methods used by virtual machinesService that provides an in-memory environment for its execution
  • with“Java Virtual Machine Stack”The same,Local method stackThere are also two types of exceptions:
    • Stack depth overflowIs thrownStackOverflowErrorabnormal
    • Stack extension failureIs thrownOutOfMemoryErrorabnormal

4. The Java heap(Java Heap)

  • The largest chunk of memory managed by a virtual machine
  • The Java heapisShared by all threadsAn area of memory
  • Sole purpose:Example of Storing objects.
    • In Java“Almost”This is where all object instances are allocated memory;
    • But, because of current technological developments, said“Java object examples are all allocated on the heap” is also becoming less absolute.
  • The Java heapisGarbage collectorManaged memory area, also known as"GC heap"
  • The Java heap can be in a physically discontinuous memory space, butLogically it should be considered continuous.
  • If theThere is no memory in the Java heap to complete the instance allocationAnd,The Java heap can no longer be extended, the Java VIRTUAL machine will throwOutOfMemoryErrorabnormal

5. Methods areaMethod (Area)

  • and“Java heap”As theShared by all threadsAn area of the city.
  • In the Java Virtual Machine Specification, theThe method area is described as a logical part of the heapBut it has a nickname called“Pile”, the purpose is withThe Java heapDistinguish.
  • ifThe method area cannot meet the new memory allocation requirementsIs thrownOutOfMemoryErrorabnormal

6. Run-time constant pool(Running Constant Pool)

  • Run-time constant poolisPart of the method area
  • Constant pool table: Used for storageCompile timeGenerated varietyliteralwithCharacter references.
    • This section will be in theAfter the class loadingDeposit toRun-time constant pool for the method areaIn the.
  • Run-time constant poolThe relativeClass file constant poolAn important feature of theDynamic.
  • whenThe constant pool could not be allocated to memoryIs thrownOutOfMemoryErrorabnormal

7. Direct memory(Direct Memory)

  • It is neither part of the run-time data area of the virtual machine nor an area of memory as defined in the Java Virtual Machine Specification.
  • But this area of memory is also frequently used and can causeOutOfMemoryErrorunusual
  1. inJDK 1.4New inNIO (New Input/Output)Class, introducedAn I/O method based on Channel and Buffer, it can be usedNative libraries Direct allocation of off-heap memoryAnd then throughA DirectByteBuffer object stored in the Java heapAs aA reference to this block of memoryPerform operations. suchCan significantly improve performance in some scenariosBecause theIt avoids copying data back and forth between the Java heap and Native heap.
  2. Native direct memory allocation is not affectedThe Java heapSize limitation, however, since it is memory, it is definitely still subject toNative total memory(including RAM and SWAP area or paging file) size andProcessor addressing spaceRestrictions. The server administrator configures VM parameters based on the actual memory-XmxAnd other parameters, but are often ignoredDirect memory, so that the sum of each memory area is greater than the physical memory limit (both physical and operating system level limits), resulting inThe dynamic extensionwhenOutOfMemoryErrorThe exception.