JVM runtime data area, common jvm exception examples

JVM runtime data area:

  • During the execution of a Java program, the Java virtual machine divides the memory it manages into several different data areas.

Heap :

  • Thread sharing
  • Storage: most created 实例对象, 数组, 字符串常量池;
  • Description:
    - The object reference is stored in , pointing to the address where the object is stored in the heap
    - The object in the heap memory stores its own member variables, and does not save the object's methods, which 方法are saved in the frame stack.
  • Parameters: -Xms1G- Initial heap memory -Xmx1G- maximum heap memory; default size
    - default initial value, 64/1the maximum physical memory 1/4; calculated based on 8G of memory, that is128M ~ 2G
  • Memory recycling: new generation, old generation - 1:2; new generation:eden 、s1 、s2 - 8:1:1
  • Recycling algorithm:
    – New generation 复制算法: Divide the available new generation memory into two areas s1 and s2. Only one area is used at a time. When one area is recycled, the surviving objects in this area are copied to another area. Empty the current area. eden + s1 - > s2 , eden + s2 ->1
    – Old generation 标记压缩: Mark all referenced objects from the root node, compress and move all surviving objects to one end of the memory, and clear the remaining garbage objects.

1
2

Heap memory overflow( java.lang.OutOfMemoryError)

List loop addition, generally may appear in:大批量添加数据

  • All general, add List in batches and import into the database
		List<User> list=new ArrayList<>();
        while(true){
    
    
            list.add(new User().setName("小树"));
        }

When processing large file IO:

//size 声明 多 ~~大 ,比如: new byte[in.available()];
byte[] buffer = new byte[size] //1024 * 1024 * 5

Exception handling: To catch this type of exception, use it Throwable e, and then handle it accordingly

		try {
    
    
			//TODO
        } catch (Throwable e) {
    
    
            e.printStackTrace();
        }

java virtual machine stack:

  • The thread private area has the same life cycle as the thread; when each method is executed, the Java virtual machine will synchronously create a stack frame (Stack Frame)
  • Storage: The stack frame stores the method's local variable table, operand stack, dynamic connection and method return address.
  • Process: The process from each method being called to the completion of execution corresponds to the process of this stack frame in the virtual machine stack from 入栈to - screenshot taken to: address出栈
  • parameter:-Xss<size>
    1

Stack overflow( java.lang.StackOverflowError):

  • The method is recursively called. The method calling level is too deep and the memory is not enough to create a new stack frame.
private static int count;

    public static void count(){
    
    
        try {
    
    
            count++;
            count();
        } catch (Throwable e) {
    
    
            System.out.println("最大深度:"+count);
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
    
    
        count();
    }

Method area: java class information, runtime constant pool, static variables

  • JDK1.7 and before are called permanent Permgeneration 堆中. Storage: Java class information, runtime constant pool, static variables, code compiled by just-in-time compiler and other data
  • JDK 1.8, called 元空间 MetaSpace, in 直接内存中. Storage: Class information and constant pools are placed in local memory, and constant pools and static variables are placed in the Java heap.
  • parameter:-XX:MaxMetaspaceSize

Program counter: Saves the address of the bytecode instruction being executed by the current thread lock; in order to return to the normal execution position after thread switching.

Native method stack: native method, JNI, uses java to call the local method library implemented in c and c++.

Guess you like

Origin blog.csdn.net/hesqlplus730/article/details/123760581