JVM memory management model

Java program execution flow

 

 

Java Memory Management

JVM runtime data area comprises:

(1) Program counter (program counter register)    

  Recording the program execution order, mark the program to be executed next sequence number code, a very small memory footprint, substantially negligible ..

(2) Java 栈 (stack)

  Save heap memory address, perform basic data, the method;

(3) native method stacks (native method stack)

  Save the native operating system function

(4) Method zone (method area)

  Save all of the specific method of operation, in the region belonging to the shared region

(5) heap (heap) 

  Save the real part of the program data

 

It is a unit runtime stack, and the stack is a storage unit

(1) Since the stack is operating unit, which information is stored with the information of the current thread (or program) includes associated local variables, program running state, and so the return value of the method;

(2) heap just save the object information

 

In five parts consisting of the entire jvm memory, stack memory is a very important part.

 

 

Java virtual machine stacks can store multiple stack frames, each stack frame also includes the following sections:

1. The local variable table: local variable or method parameter, which is variable groove (slot) as a minimum unit, is allowed to save the 32-bit variable length, if it exceeds 32, two successive grooves (64 will open length, long, double)

2. The operand stack (operand Stack): where instruction execution

3. The method of the current operating point belongs to the class time constant pool reference (reference to runtime constant pool): referencing the other classes or string constant pool String;

The method of the return address (return address): The method of implementation of the latter need to return to the position of calling this method, it is necessary to save the return address stack frame method

 

 

Stack There are two common exceptions

1. java.lang.StackOverflowError referred to as SOE, when requesting a stack depth is too large will throw this exception, the ratio of recursive calls

public class StackTest {
    public static  int count = 0;
    public static void main(String[] args) {
        print();
    }
    public static void print() {
        System.out.println(count++);
        print();
    }
}
Exception in thread "main" java.lang.StackOverflowError
	at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:691)
	at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
	at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
	at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
	at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
	at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
	at java.io.PrintStream.write(PrintStream.java:526)
	at java.io.PrintStream.print(PrintStream.java:597)

  

 

 

2. 如果虚拟机的实现中允许虚拟机动态扩展,当内存不足以扩展栈的时候,会抛出OutOfMemoryError异常,简称OMM

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12208023.html