JVM: Java area of memory and memory overflow exception

Java virtual machine memory division during the execution of the Java program in which it will manage for a number of different data areas. These regions have their own purposes, and the creation and destruction of time, in some regions as the process of starting the virtual machine exists in some regions dependent on start and end users to create and destroy threads while.

Java virtual machine runtime data area:

Program Counter:

Is a small memory space, can be seen that the current thread executed bytecode line number , when the bytecode interpreter is working by changing the value of this byte code counter to select a next instruction to be executed, the branch , looping, branching, exception handling, thread recovery will need to rely on the counter.

If the execution of a Java method , this counter is to record the address of the bytecode instructions being executed ; if the Java Native method , the counter value is null . In addition, this area is a Java Virtual Machine Specification only not an OutOfMemoryError area.

Java virtual machine stack:

And program counter , like a thread private, life cycle and the same thread, it is described memory model of Java execution method , each method is created while executing a stack frame (for storing local variable table, the operand stack ,), dynamic link pushed onto the stack, like the method outlet, each method when executed, a corresponding stack frame from the virtual machine pops the stack.

Local variable table stored in the compile-time type are known various basic data, a reference (reference to the object pointer or handle to its associated address) and returnAddress type of object. Local variable table requires memory space allocation done during compilation, when entering a method, the stack frame allocated much required local variable table space is determined, the size does not change during operation.

Java Virtual Machine Specification defines this there are two exceptions: StackOverflowError (deeper than the depth of the stack when the stack of requests) and OutOfMemoryError (dynamically extensible but when sufficient internal space).

Native method stacks:

And virtual machine stack to play a role very similar between them is the difference between merely virtual machine stack virtual machine executes Java method (bytecode) services, native method stacks , the virtual machine to use the Native Methods service.

The language of the native method stacks in the methods used, data structures, and not a mandatory requirement in the virtual machine specification, the virtual machine can be free to implement it. HotSpot virtual machine directly to the native method stacks and stack virtual machines combined. As you might throw exceptions and virtual machine stack.

Java heap (Heap):

It is a virtual machine memory management in the largest one place. Java heap is shared by all threads in a memory area in the virtual machine startup is created. Its purpose is to store instances of Java objects will (almost all instances where memory allocation).

It is also the main area managed by the garbage collector. Now collectors have adopted the basic generational algorithm , Java heap can be divided into the new generation and the old year , and then there is a little thin Cenozoic Eden space, From Survivor space, To Survivor space.

Mention here permanently with  the PermGen (metadata storage class in the heap, the virtual machines are not all permanently with the concept) , the HotSpot VM present in the prior version 8 JDK, JDK 8 and after, with permanent It is removed, relative to the introduction of a new memory area (local memory) called metaspace (element space).

The reason Java 8 is deleted from the PermGen:

PermGen has many drawbacks:

  • Start a fixed size: difficult to adjust, the required size depends on the number of classes, the size of the method, the size of the constant pool.
  • Hot Java object type is internal: the complete GC can be moved, opaque, move together is not strongly typed and difficult to debug metadata.
  • Simplified complete set: For each set of iterators for adding special metadata.
  • Now, no concurrent GC pause, you can cancel the assigned class data at the same time.
  • PermGen limit future improvements achieved.

MetaSpace advantages:

In terms of performance and memory management, MetaSpace has many advantages:

  • Using Java Specification properties: Survival of metadata associated with the classes and class loader match.
  • Each loader storage area: metaSpace
  • Only linear distribution.
  • There is no separate recovery (re-defined classes and failed to load excluded)
  • GC does not scan or compression.
  • No metaverse object relocation.

Dimensional space adjustment

To set the maximum dollar amount of space, you can use -XX: MaxMetaspaceSize signs, and by default, according to the machine's memory limit is unlimited. If this flag is not specified maximum limit, the Metaspace dynamically resize based on the application needs at runtime.
This change will enable optimization and other features in the future

    • Application class data sharing.
    • Younger collectors optimization, G1 class unloading.
    • Reduce the size of the metadata and internal JVM space projects.

Method area:

And the Java heap, as each thread is a shared memory area for storing virtual machine loads class information, constants, static constant, the immediate compiled code and other data. Java Virtual Machine specification to distinguish the method area (Non-Heap) and the Java heap. OutOfMemoryError will run out of memory when not met.

Runtime constant pool:

Region part of the method. In addition to the Class file versions have tired, fields, methods, and interface description information, there is a constant pool (Constant Pool Table), and for storing various literal compile generated reference symbols, this part will be the method of operation of the access zone of the storage class loading time constant pool.

 

 

Guess you like

Origin www.cnblogs.com/magic-sea/p/11610532.html