"Heap overflow processing" in one of the actual Java virtual machines

Source: Internet
Author: User

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:

    1. Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
    2. 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.


    1. public class  dumpoom {  
    2.  public  static void main (string[] args)  {  
    3.  vector v=new vector ();   
    4.   for (int i= 0;i<25;i++)   
    5.    v.add (new byte[ 1*1024*1024]);   
    6.  }  
    7. }&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:


    1. Java.lang.OutOfMemoryError:Java Heap Space
    2. Dumping heap to d:/a.dump ...
    3. Heap dump file created [23067302 bytes in 0.160 secs]
    4. Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
    5. 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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.