Related Memory Knowledge descriptionThe method area holds the loaded class information
- Constant pool of classes
- field, method information
- Method byte code
Common and permanent (Perm) associated with the Java heap
- and program development is closely related
- Application system objects are saved in the Java heap
- All threads share the Java heap
- For a generational GC, the heap is also generational
- GC's main working range
Java stack
- Thread Private
- Stacks are made up of a series of frames (so Java stacks are also called frame stacks)
- Frame saves a method's local variables, operand stacks, Chang pointers
- Each time the method call creates a frame and presses the stack
Under code description, memory allocation during virtual machine execution
Public classApp//at runtime, the JVM puts the APP's information into the method area{ Public Static voidMain (string[] args)//The Main method itself is put into the method area. {Sample test1=NewSample ("Xiao Ming"); //Test1 is a reference, so put it in the stack, sample is the object placed in the heapTest1.sayhello (""); Sample.runstatic (3, 1, 1.0,NULL); }}
Methods for executing classes
Public classSample {//at runtime, the JVM puts the sample information into the method area PrivateString name; //After the new sample instance, the name reference is placed in the stack, and the name object is placed in the heap PublicSample (String name) { This. Name =name; } //The Print method itself is placed in the method area. //each thread invokes the change method, resulting in a new "stack frame" Public voidSayHello (intAintb) { //after entering this method//1 local variable referencethis (reference to itself object) the stack is stored in the stack frame//2 local variable int a the stack is stored in the stack frame//3 local variable int b press Stack//4 local variable int c intc = 0; C= A +b; //Java does not have a register, all parameters are passed using the "operand stack"//processing of operand stacks during method execution//1. Press the value 0 to stack (the operand stack)//2. Eject int to store local variable C//3. Press the local variable a stack//4. Press the local variable b stack//5. The sum of two variables is popped, and the result is stacked, and a A, b in the value stack is cleared. //6. Popup summation result, put with local variable C//7. Press the local variable C stack into the stack frame//other instructions, c=0++; the execution process//1. Press the value 0, execute the + + action directly, and return the values to the C//so i++ than i=i+1; fast execution SpeedSystem.out.println (c); } //method ends clean off SayHello stack frame//therefore, the stack space does not require garbage collection.
Static methods are slightly different
Example of a recursive method Runstatic (2) His stack space and execution
Each recursion will increase the stack space memory,
Therefore, the size of the stack space determines the depth of the method invocation.
Case Proof:
Public classTeststackdeep {Private Static intCount=0; Public Static voidRecursion (LongALongBLongc) { Longe=1,f=2,g=3,h=4,i=5,k=6,q=7,x=8,y=9,z=10; Count++; Recursion (A,B,C); } Public Static voidMain (String args[]) {Try{recursion (0l,0l,0l); }Catch(Throwable e) {System.out.println ("Deep of calling =" +count); E.printstacktrace (); } }}
When the stack size is set to 128k, the recursive call is made 701 times,
When the stack size is set to 256k, the recursive call is made 1817 times,
It can be seen that the small stack space will affect the hierarchy of recursive calls, but the allocation is too large, but also affect memory consumption, reduce the amount of thread concurrency.
Configure-xmx20m-xms5m-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=d:/a.dump
outputting information to a dump file when Oom is in place
Vector v=new vector (); for (int i=0;i<25;i++) v.add (newbyte[1*1024*1024]);
The resulting heap space exceeds the maximum space overflow, and the output object is exactly the configured maximum memory size. 19M, the 20th can not be assigned, so an error.
More memory usage, GC information can be xx:+printgcdetails output, view the details of each memory block usage.
In this case, a portion of the memory is large to the maximum value. There are other areas, such as memory overflows in the permanent zone
Some other memory overflow scenarios are listed below"Situation One":
Java.lang.OutOfMemoryError:Java Heap Space: This is the Java heap memory is not enough, one reason is really not enough, another reason is that the program has a dead loop;
If the Java heap memory is not enough, you can fix it by adjusting the configuration under the JVM:
< jvm-arg>-xms3062m </jvm-arg>
< jvm-arg>-xmx3062m </jvm-arg>
"Situation two"
Java.lang.OutOfMemoryError:GC Overhead limit exceeded
"Explanation": JDK6 adds a new error type that is thrown when the GC is taking a lot of time to free up a small space, usually because the heap is too small to cause an exception and there is not enough memory.
"Solution":
1, check whether the system has the use of large memory code or a dead loop;
2. Limit the use of memory by adding a JVM configuration:
< jvm-arg>-xx:-usegcoverheadlimit</jvm-arg>
"Situation three":
Java.lang.OutOfMemoryError:PermGen space: This is a P-zone memory that can be configured by adjusting the JVM:
< jvm-arg>-xx:maxpermsize=128m</jvm-arg>
< jvm-arg>-xxermsize=128m</jvm-arg>
"Note":
The perm area of the JVM is primarily used for class and meta information, and class is loader to PermGen space, which becomes an old generation, and the GC does not clean up the old age area during the main program run, which defaults to 64M size. When the program needs to load more than a few objects, more than 64M will be reported this part of memory overflow, need to increase memory allocation, general 128m enough.
"Situation four":
Java.lang.OutOfMemoryError:Direct Buffer Memory
Adjust the-xx:maxdirectmemorysize= parameters, such as adding a JVM configuration:
< jvm-arg>-xx:maxdirectmemorysize=128m</jvm-arg>
"Situation five":
Java.lang.OutOfMemoryError:unable to create new native thread
Reason: Stack space is not enough to create additional threads, either too many threads are created, or the stack space is indeed small.
"Resolution": Because the JVM does not provide parameters to set the total stack space size, but can set the size of a single line stacks, and the system's user space is 3G, in addition to text/data/bss/memorymapping a few paragraphs, heap and stack space is limited , it is the elimination of the other long. As a result, there are two ways to solve this error:
1. Reduce the size of a single thread stack by-XSS startup parameters, so that more threads can be opened (not too small, too small to appear stackoverflowerror);
2. Reduce the heap size by-xms-xmx two parameters and give the memory to the stack (provided that the heap space is sufficient).
"Situation six":
Java.lang.StackOverflowError
"Cause": This is also a memory overflow error, that is, the overflow of the line stacks, either the method call hierarchy too many (such as there is an infinite recursive call), or the line stacks is too small.
"Solve": Optimize the program design, reduce the method call level, adjust the-XSS parameter to increase the thread stack size.
This article is intended to briefly describe the analysis. The depth is very limited.
Java Memory Overflow Analysis