Java memory area and memory overflow exception one, run-time data region
1. Program counter: Thread private, used to store the currently executing command location
2.Java virtual machine stack: Thread-Private, describe the Java method execution model, execute the method will create a stack frame, store local variables, basic type variables, references and other information
3.Java Local method Stack: Thread-Private, native method service for virtual machine use
4.Java heap: Thread sharing, is the main work place of garbage collector, storage object instance, etc.
5. Method area: Thread sharing, storage class information, constants, static variables, etc.
Run constant: Holds various literal and symbolic references generated at compile time
6. Direct memory: The memory of the machine
Ii. Virtual Machine Objects 1. Creation of objects
- First, check whether the constant pool can locate the symbolic reference of this class, and check whether the class has been loaded or not, or perform the loading process first;
- Allocate memory for an object: Calculates the space and divides a contiguous or discontinuous area from the heap, using cas+ failed retry to avoid thread safety issues (because object creation is very frequent and it is not known that the existing entities are not assigned)
- Initialize memory space: Initialize the allocated memory space by 0 values
- Set object basic information: metadata, hash code, GC, etc.
- Perform the init initialization of Java:
2. Memory layout of objects
Object header: The hash code of the stored object, the lock state, and the type pointer (the metadata of the class that the object points to)
Instance data: Information that the object actually stores
Align fills: Fill in compliance with rules
3. Object access and positioning
Object access, through the reference data on the Java stack, which maintains a reference to the object
Access mode: Handle and direct access
Handle: The handle pool is maintained in the heap, reference points to the handle, and the handle contains address information for object instance data and type data
Easy to move, modify the instance data in the handle directly; overhead, one more pointer positioning
Direct: Reference direct point to object address
Fast speed
Third, actual combat outofmemoryerror 1.java heap Overflow
parameters :-xms heap minimum value;-xmx heap maximum;-xx:+heapdumponoutofmemoryerror memory snapshot analysis when overflow occurs
Objects are stored in the heap: You can create a large number of objects to implement heap overflow: heap space
2. Stack Overflow
parameter :-XSS Set Stack value
Stack depth, can be increased by infinite recursion, or create a large number of thread implementations
//return Stackoverflower Public classjavavmstacksof {Private intStacklength = 1; Public voidStackleak () {stacklength++; Stackleak ();} Public Static voidMain (string[] args)throwsthrowable{javavmstacksof oom=Newjavavmstacksof ();Try{oom.stackleak ();}Catch(Throwable e) {System.out.println ("Stack Length:" +oom.stacklength);Throwe; }}}
3. Method area and constant pool overflow
parameters :-xx:permsize method area size;-xx:maxpermsize method Area maximum Size
Before JDK1.6, you can overflow by creating a large number of string, virtual-opportunity copy objects into a constant pool
In 1.7 and later, this is not possible because the virtual machine will only save a reference to the object when this object first appears in the constant pool
Method Area overflow: The method area holds information about the class and overflows by generating a large number of dynamic classes, such as spring, which is also the class generated by the dynamic proxy.
Public classjavamethodareaoom{ Public Static voidMain (String[]args) { while(true){//Create a large number of dynamic classes, dynamic proxy oomobjectEnhancer enhancer=Newenhancer (); Enhancer.setsuperclass (Oomobject.class); Enhancer.setusecache (false); Enhancer.setcallback (NewMethodinterceptor () { PublicObject Intercept (object Obj,method method,object[]args,methodproxy proxy)throwsthrowable{returnProxy.invokesuper (Obj,args);}} ); Enhancer.create ();}}Static classoomobject{}}
String.intern () is a native method that, if the string constant pool already contains a string equal to this string object, returns a String object that represents the string in the pool, otherwise the string contained in this string object is added to the constant pool and returns this str Reference to ING object
JDK6 and Previous: Method area (permanent generation) is separate, constant pool within method area
JDK7: Go to the permanent generation
Public class runtimeconstantpooloom{ publicstaticvoid Main (String[]args) { String str1=new StringBuilder ("Computer"). Append ("Software"). toString (); System.out.println (Str1.intern ()= =str1); String str2=new StringBuilder ("ja" ). Append ("va"). toString (); System.out.println (Str2.intern ()= =str2) ;
This code runs in JDK 1.6, gets two false, and runs in JDK 1.7 and gets a true and a false.
The difference is due to the fact that in JDK 1.6, the Intern () method copies the first encountered string instance to the permanent generation, returns a reference to the string instance in the permanent generation, and the string instance created by StringBuilder on the Java heap, so it is not necessarily the same reference , False is returned.
The JDK 1.7:intern () implementation does not replicate the instance, only the first occurrence of the instance reference is recorded in the constant pool, so the reference returned by intern () and the string instance created by StringBuilder are the same.
The str2 comparison returns false because the string "Java" has already occurred before execution of Stringbuilder.tostring (), the string constant pool already has its reference, does not conform to the "first appearance" principle, and "Computer software" This string is the first occurrence and therefore returns true
Note: 1.7 and later are the first occurrences of the reference; Understanding the above analysis
4. Native Direct Memory
Parameters:-xx:maxdirectmemorysize Direct memory size; default = = Max heap Memory
Learn more about Java Virtual machines (1) Java Memory areas and memory overflow exceptions