Starting today, I will send 5 small series about Java virtual machines:
"Heap overflow processing" in one of the actual Java virtual machines
Virtual machine working mode for Java Virtual machine
The three "G1 of the New generation GC" of Java Virtual machine
Live Java Virtual machine Four "Disable System.GC ()"
Real-time Java Virtual Machine Five "turn on JIT compilation"
Let's talk about "one of the actual Java virtual machines" heap overflow processing ""
In the course of running a Java program, if there is insufficient heap space, it is possible to throw a memory overflow error (out of Memories), which is referred to as oom. The following text shows a typical heap memory overflow:
-
exception in thread "Main" java.lang.outofmemoryerror: java heap space
-
at geym.zbase.ch3.heap.dumpoom.main (Dumpoom.java:20
Once such problems occur, the system is forced to exit. If it occurs in a production environment, it 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 get as much field information as possible in the event of an error, in order to help the developers to troubleshoot the site problem. The Java Virtual machine provides the parameter-xx:+heapdumponoutofmemoryerror, which allows you to export the entire heap information when memory overflows. In conjunction with-xx:heapdumppath, you can specify a storage path for the exported heap.
"Example 3-4" The following code total allocates 25M of memory space.
-
public class DumpOOM {
-
public static void
-
Vector v=new vector ();
-
< span class= "keyword" style= "Margin:0px;padding:0px;border:none;color:rgb (0,102,153); background-color:inherit; Font-weight:bold; " >for ( int 0 25
V.add (New byte[1*1024x768*1024x768]);
}
}
Execute the above code using the following parameters:
-xmx20m-xms5m-xx:+heapdumponoutofmemoryerror-xx:heapdumppath=d:/a.dump
Obviously 20M heap space is not enough to accommodate 25M of memory, such as a memory overflow, after an error occurs, the console output is as follows:
Java.lang.OutOfMemoryError:Java Heap Space
Dumping heap to d:/a.dump ...
Heap dump file created [23067302 bytes in 0.160 secs]
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
-
at Geym.zbase.ch3.heap.DumpOOM.main (Dumpoom.java:19
"Practical Java Virtual Machine-JVM fault diagnosis and performance optimization"
650) this.width=650; "class=" Alignnone size-medium wp-image-565 "src=" http://www.uucode.net/wp-content/uploads/2015 /04/jvm1-300x296.png "alt=" jvm1 "width=" "height=" 296 "style=" border:0px None;padding:0px;margin:0px;list-style: None;height:auto; "/>
In addition to exporting heap information when an oom occurs, the virtual machine also allows a script file to be executed when an error occurs. This file can be used for self-help, alarms, or notifications of the crash program, and also helps developers get more information about the system, such as a full thread dump (that is, thread dumps or core dump) files.
This gives an example of exporting thread dumps when an oom occurs. Prepare the Printstack.bat script as follows:
D:/tools/jdk1.7_40/bin/jstack-f%1 > D:/a.txt
The above script will export the thread information for the given Java Virtual machine process and save it in the D:/a.txt file.
Execute the above code using the following parameters:
-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" for the author's JDK according to the directory, readers can replace their own java_home directory, to try.
"Heap overflow processing" in one of the actual Java virtual machines