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:)
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 main (string[] args) {
- vector v=new vector ();
- for (int i= 0;i<25;i++)
- v.add (new byte[ 1*1024*1024]);
- }
- }&NBSP;&NBSP;
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: +)
As you can see, the virtual machine exports the current heap and saves it to the D:/a.dump file. Using tools such as the mat to open the file for analysis, it is easy to find these byte arrays and save their vector object instances. For the use of tools such as Mat, you can refer to the "real-life Java Virtual machine-JVM Troubleshooting and Performance Optimization" chapter 7th.
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.
One of the practical Java virtual machines "heap overflow processing"