You can open wechat and search for MelodyJerry

By the author in this paper the original [JVM | memory model] Java virtual machine memory model? Also only these 7 modifications come from, can click on the lower left corner to read the original text.

JVM memory model/memory space

When the Java Virtual Machine (JVM) is running, the memory is allocated a space called the runtime data area.

The runtime data area is mainly divided into the following six 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!!

Program counter can also be called PC register, commonly known as instruction cache, it is mainly used to cache the current program to execute the next instruction address, according to this address CPU to find the instruction to be executed. This register is implemented internally by the JVM. It is not a physical counter, but it is implemented logically by the JVM.

2.Java virtual machine stack(VM Stack)

  • A thread-memory model for Java method execution
  • Each threadEverything that’s up and runningCorresponding to a stack (thread stack).Data on the stackisUnique to this threadThe,Don'tproduceResource sharingThe case, thereforeThread stacks are thread-safe.
  • What’s in the stack isThe stack frame
    • eachJava methodThe execution of aThe stack frametheInto the stackandOut of the stackThe operation of the
    • whenThread call method, forms aThe stack frameAnd put thisThe stack frameforThe pressure of stackOperation,After the method is executedforOut of the stackOperation.
    • This stack frame contains:A local variable,The operand stack,A constant pool reference to the class corresponding to the current method,Method return addressInformation such as
  • Run this command for the VMJava method(i.e.The bytecode) service
  • Thread privateThe memory of
  • itsThe life cycle is the same as that of a thread
  • 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 Stack)

  • 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
      • Local methodThe JVM needs to callThe Java languageMethods implemented, such as C/C++/C#
    • The JVM stackRunning aJava method

In the JVM specification, there is no enforcement requirement that an implementor must separate the local method stack (for example, the HotSpot VIRTUAL machine combines the local method stack with the stack) from the implementation (the JVM specification is implemented differently for different operating systems).

  • 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".
    • Heap memoryIn theObject is not referencedWill,automaticThe JavaGarbage collection mechanismRecycling.
  • When defined in the methodA local variable:
    • If the local variable isBasic data types, stored directlyStack memory;
    • If the local variable isReference data typeWill,A variable's valueStored in aHeap memory,Stack memoryOnly in the storeRefer to the address.
  • 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.
  • Mainly for each oneIs loadedtheclassThe information of

The class information mainly contains magic numbers (to determine whether it is a class file), constant pools, and access flags (whether the current class is a common class or an interface, whether it is abstract, whether it is modified by public, whether it is modified by final, etc…….). , field table set information (what access modifiers are used, whether they are instance variables or static variables, whether final modifiers are used and other descriptors…..) , method table set information (which access modifier is used, whether static method, whether final modifier is used, whether synchronized modifier is used, whether native method……) And so on.

When a class loader loads a class, it creates a class object from the class file that contains the above information. Subsequent instances of this class are created from this class object.

  • 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.
  • Store the most important resources in the classEach class objectallmaintenancetheA constant pool.
  • 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

Direct memory [special]

The runtime data area is dominated by these six areas, but there is a more specific area managed by the JVM:

  • 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.

References:

[1] The memory space of the JVM