Start today. I will send 5 small series about the Java Virtual machine:
-
- practical Java Virtual Machine two" virtual machine working mode "
- real-G1 Java Virtual machine three" new Generation GC "
- Live Java Virtual machine Four" Disable System.GC () "
- real-time Java Virtual Machine Five" turn on JIT compilation
Here is a "heap overflow processing" in the actual Java virtual machine
In the execution of a Java program, assume that there is insufficient heap space. It is possible to throw a memory overflow error (out of memories), referred to as oom. For example, the following text shows a typical heap memory overflow:
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap spaceat Geym.zbase.ch3.heap.DumpOOM.main (Dumpoom.java : 20)
Once such problems occur. The system will be forced to exit. Assumptions occur in the production environment. can cause serious business disruption.
In order to be able to continuously improve the system, to avoid or reduce the occurrence of such errors, you need to obtain as much information as possible in the event of an error, to help the developers to troubleshoot the site problems. The Java Virtual machine provides the parameter-xx:+heapdumponoutofmemoryerror, which allows the entire heap information to be exported when memory overflows.
And it is used in conjunction with-xx:heapdumppath. The ability to specify the storage path for the exported heap.
"Demo Sample 3-4" The following code totals allocate 25M of memory space.
public class Dumpoom {public static void main (string[] args) {vector v=new vector (); for (int i=0;i<25;i++) v.add (new byte[1*1024*1024]);}}
Run the above code using the following parameters, for example:
-xmx20m-xms5m-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=d:/a.dump
Obviously 20M heap space is not enough to accommodate 25M of memory. For example, a memory overflow occurs, and after an error occurs, the console outputs such as the following:
Java.lang.OutOfMemoryError:Java heap spacedumping heap to d:/a.dump ... Heap dump file created [23067302 bytes in 0.160 secs]exception in thread "main" Java.lang.OutOfMemoryError:Java Heap Spac Eat Geym.zbase.ch3.heap.DumpOOM.main (dumpoom.java:19)
you can see that the virtual machine exports the current heap and saves it to the D:/a.dump file. Use tools such as the mat to open the file for analysis, as seen in. It is very easy to find these byte arrays and the vector object instances that hold them. About the use of tools such as Mat. Can read"Practical Java Virtual Machine-JVM fault diagnosis and performance optimization"7th chapter.
In addition to the ability to export heap information when an oom occurs, the virtual machine also agrees to run a script file when an error occurs. This file can be used for self-rescue, alarm or notification of the crash program. It also helps developers to get a lot of other system information, such as a full thread dump (that is, thread dumps or core dump) files.
Here is a sample that exports thread dumps when an oom occurs.
Prepare Printstack.bat scripts such as the following:
d:/tools/jdk1.7_40/bin/jstack-f%1 > D:/a.txt
The above script will export thread information for a given Java Virtual machine process. and saved in the D:/a.txt file.
Run the above code using the following parameters, for example:
-xmx20m-xms5m "-xx:onoutofmemoryerror=d:/tools/jdk1.7_40/bin/printstack.bat%p"-xx:+ Heapdumponoutofmemoryerror-xx:heapdumppath=d:/a.dump
When the program exits abnormally, a new file a.txt is generated under the System D, which holds the thread dump information. In this example, the file path "D:/tools/jdk1.7_40" is the author's JDK according to the folder. The reader can replace it with its own Java_home folder and try it out.
"Real-combat Java Virtual Machine" a book Q Exchange Group: 397196583
A "heap overflow processing" in the actual Java virtual machine