Home >Java >javaTutorial >Detailed introduction to java memory area and memory overflow exception
1. Program counter: thread private , used to store the location of the currently executed instruction
2. Java virtual machine stack: thread private, describing the Java method execution model; when executing a method, a stack frame will be created to store local variables and basic type variables. Reference and other information
3.Java native method stack: thread private, serving the Native methods used by the virtual machine
4.Java heap: thread shared, is the main working place of the garbage collector ;Storage object instances, etc.
5. Method area: thread sharing; storage class information, constants, static variables, etc.
Runtime constants: store various literals and symbol references generated during compilation
6. Direct memory: machine memory
Check first Can the constant pool locate the symbol reference of this class and check whether the class has been loaded and initialized, otherwise the loading process must be performed first;
Allocate memory for the object: calculate the space and extract it from the heap Divide a continuous or discontinuous area; use cas+failure retry to avoid thread safety issues (because objects are created very frequently, I don’t know whether the current memory has been allocated)
Initialize memory space: Initialize the allocated memory space to 0 value
Set basic information of the object: metadata, hash code, gc, etc.
Execute Java's init initialization:
Object header: stores the hash code, lock status, etc. of the object and the type pointer (the class pointed to by the object) Metadata)
Instance data: the information actually stored by the object
Alignment filling: filling conforms to the rules
Object access , through the reference data on the java stack, it maintains a reference to the object
Access method: Handle and direct access
Handle: A handle pool is maintained in the heap, reference points to the handle, and the handle contains the address information of the object instance data and type data
It is easy to move, just modify the instance data in the handle directly; the overhead is high, and there are more One-time pointer positioning
Direct: reference points directly to the object address
##
三、actual combat OutofMemoryERROR .java heap overflow
Parameters
: -Xms heap minimum value; -Xmx heap maximum value; -XX:+HeapDumpOnOutOfMemoryError Memory snapshot analysis when overflow occurs Heap storage Objects: A large number of objects can be created to achieve heap overflow: heap space2. Stack overflow
Parameter
: -Xss sets the stack value Stack depth can be achieved by increasing the stack depth through infinite recursion or creating a large number of threads//递归来StackOverFlowerpublic class JavaVMStackSOF {private int stackLength = 1;public void stackLeak(){ stackLength++; stackLeak(); }public static void main(String[] args)throws Throwable{ JavaVMStackSOF oom = new JavaVMStackSOF();try { oom.stackLeak(); } catch(Throwable e){ System.out.println("stack length:" + oom.stackLength);throw e; } } }
3. Method area and constant pool overflow
Parameters :-XX:PermSize method area size; -XX:MaxPermSize maximum size of method area
Before JDK1.6, you can create a large number of Strings, and the virtual machine will copy the objects and put them into the constant pool, thereby overflowing ##
In 1.7 and later, this cannot be done, because the virtual machine will only save the reference to the object when the object first appears in the constant poolpublic class JavaMethodAreaOOM{public static void main(String[]args){while(true){//创建大量的动态类,动态代理OOMObjectEnhancer enhancer=new Enhancer(); enhancer.setSuperclass(OOMObject.class); enhancer.setUseCache(false); enhancer.setCallback(new MethodInterceptor(){public Object intercept(Object obj,Method method,Object[]args,MethodProxy proxy)throws Throwable{return proxy.invokeSuper(obj,args); }} ); enhancer.create(); }}static class OOMObject{ } }
String.intern() is a Native method. Its function is: if the string constant pool already contains a string equal to this String object, return the String object representing the string in the pool; Otherwise, add the string contained in this String object to the constant pool, and return the reference of this String object
JDK6 and before: the method area (permanent generation) is separate, and the constant pool is in the method area
public class RuntimeConstantPoolOOM{public static void main(String[]args){ String str1=new StringBuilder("计算机").append("软件").toString(); System.out.println(str1.intern()==str1); String str2=new StringBuilder("ja").append("va").toString(); System.out.println(str2.intern()==str2); } }###
When this code is run in JDK 1.6, it will get two false, but when it is run in JDK 1.7, it will get one true and one false.
The reason for the difference is: in JDK 1.6, the intern() method will copy the first encountered string instance to the permanent generation, and return a reference to the string instance in the permanent generation, and The string instance created by StringBuilder is on the Java heap, so it must not be the same reference, and false will be returned.
And JDK 1.7: the intern() implementation will no longer copy the instance, but only records the first instance reference of in the constant pool, so the reference returned by intern() is the same as the one created by StringBuilder That string instance is the same.
The comparison of str2 returns false because the string "java" has already appeared before executing StringBuilder.toString(), and there is already a reference to it in the string constant pool, which does not meet the requirements of "first occurrence" principle, and the string "computer software" appears for the first time, so true is returned
Note: 1.7 and later save the reference that appears for the first time; understand the above analysis
Parameters: -XX: MaxDirectMemorySize direct memory size; default == maximum heap memory
The above is the detailed content of Detailed introduction to java memory area and memory overflow exception. For more information, please follow other related articles on the PHP Chinese website!