Java memory overflow and java Overflow
1. java Heap Overflow
JAVA heap is used to store object instances. As long as you constantly create objects and ensure that GC Roots directly have accessible paths to objects, so that the garbage collection mechanism does not know the objects clearly, memory overflow may occur.
The following code limits the Java heap size to 1024 MB, and the-Xms and-Xmx parameters are the same, that is, they cannot be expanded. Add VM parameters to Run Configuration:
-XX:+HeapDumpOnOutOfMemoryError
Let the VM Dump out the current memory heap snapshot when a memory overflow exception occurs.
package com.jvm.exception;
import java.util.ArrayList;
import java.util.List;
public class HeapSpaceException {
public static void main(String[] args) {
List<byte[]> byteList = new ArrayList<byte[]>();
while(true){
byteList.add(new byte[1024]);
}
}
}
Running result:
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid2783.hprof ...
Heap dump file created [3795151132 bytes in 3.289 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.jvm.exception.HeapSpaceException.main(HeapSpaceException.java:13)
In the memory, the system first prompts an OutOfMemoryError to upgrade the Java heap space.
Generally, Memory analysis tools such as Eclipse Memory Analyzer can be used for analysis.
If the memory leaks, view the reference chain of the leaked object to GC Roots to determine the memory leak location.
If the memory is not leaked, check the virtual machine parameters (-Xmx and-Xms ).
2. Virtual Machine stack and local method Stack Overflow
Because the HotSpot virtual machine does not distinguish between the virtual machine stack and the local method stack, the-Xoss (set the local method stack size) parameter exists for the HotSpot, but it is actually invalid. Stack capacity can only be set using the-Xss parameter. The Virtual Machine stack and local method stack define two exceptions in the Java Virtual Machine specification:
-1. If the thread request stack depth is greater than the maximum depth allowed by the virtual machine, an StackOverFlowError error is thrown.
-2. If the VM cannot apply for sufficient memory space when looking for an extended stack, an OutOfMmemoryError is thrown.
First, in a single-threaded environment, the-Xss parameter is used to reduce the stack memory capacity and StackOverFlowError is thrown.
package com.jvm.exception;
/**
* Default-Xss Parameter
* @author jinglongjun
*
*/
public class StackOverFlowError {
private int stackLength = 1;
public void stackLeak(){
stackLength ++;
stackLeak();
}
public static void main(String[] args) {
StackOverFlowError sof = new StackOverFlowError();
try{
sof.stackLeak();
}catch(Throwable e){
System.out.println("stackLength : " + sof.stackLength);
throw e;
}
}
}
Output content:
stackLength : 10828
Exception in thread "main" java.lang.StackOverflowError
at com.jvm.exception.StackOverFlowError.stackLeak(StackOverFlowError.java:13)
at com.jvm.exception.StackOverFlowError.stackLeak(StackOverFlowError.java:14)
The single-threaded Environment defines a large number of local variables and increases the length of the local variable table in the method stack. When StackOverFlowEoor is thrown, the output stack depth is reduced accordingly.
This situation has not been simulated for a long time. This is because this article refers to the book "deep understanding of Java Virtual Machine", which does not provide examples.
StackOverFlowError is always thrown in a single thread.
Multi-thread simulation.
We recommend that you do not try to run the following code.
We recommend that you do not try to run the following code.
We recommend that you do not try to run the following code.
package com.jvm.exception;
public class OutOfMemoryError {
public void dontStop(){
while(true){
}
}
public void stackLengthThread(){
while(true){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
dontStop();
}
});
thread.start();
}
}
public static void main(String[] args) {
OutOfMemoryError oome = new OutOfMemoryError();
oome.stackLengthThread();
}
}
The above code will throw:
OutOfMemoryError.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.