"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:

    • actual Java Virtual machine one" heap overflow processing "
    • actual Java Virtual machine two" virtual machine working mode "
    • actual Java Virtual Machine three" G1 of the New generation GC "
    • actual Java Virtual Machine four" Disable System.GC () "
    • Practical 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 spaceat 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 main (string[] args) {vector v=new vector ();  for (int i=0;i<25;i++)   v.add (new byte[1*1024*1024]);}}


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 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)

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, refer to"Practical Java Virtual Machine-JVM fault diagnosis and performance optimization"7th Chapter.

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.