One of the actual Java virtual machines is "Heap Overflow processing ".

Source: Internet
Author: User

One of the actual Java virtual machines is "Heap Overflow processing ".

From today on, I will publish five small series about Java virtual machines:

  • One of the actual Java virtual machines is "Heap Overflow processing"
  • Practice: Java Virtual Machine 2 "virtual machine working mode"
  • Java Virtual Machine 3: New Generation GC of G1"
  • 4. Disable System. gc () for Java virtual machines ()"
  • 5. "enable JIT compilation"

Next let's talk about [one of the actual Java virtual machines is "Heap Overflow processing "]

During Java program running, if the heap space is insufficient, an Out Of Memory (OOM) error may be thrown. 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. If it occurs in the production environment, it may cause serious service interruption. In order to continuously improve the system and avoid or reduce the occurrence of such errors, we need to obtain as much field information as possible in case of errors to help R & D personnel troubleshoot on-site problems. The Java virtual machine provides the parameter-XX: + HeapDumpOnOutOfMemoryError, which can be used to export the entire heap information when the memory overflows. It also works with-XX: HeapDumpPath to specify the storage path of the exported heap.

[Example 3-4] the following code allocates a total of 25 MB 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 preceding code using the following parameters:

-Xmx20m-Xms5m-XX: + HeapDumpOnOutOfMemoryError-XX: HeapDumpPath = d:/a. dump

Obviously, 20 mb of heap space is insufficient to accommodate 25 MB of memory. For example, if a memory overflow occurs in the system, the console outputs The following after an error occurs:

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 spaceat geym.zbase.ch3.heap.DumpOOM.main(DumpOOM.java:19)

As you can see, the VM exports the current heap and saves it to the D:/a. dump File. Using tools such as MAT to open the file for analysis, you can easily find these byte arrays and save their Vector object instances. For details about how to use tools such as MAT, refer to Chapter 7th "Practical Java Virtual Machine-jvm Fault Diagnosis and performance optimization.

In addition to exporting heap information when OOM occurs, the virtual machine can also execute a script file when an error occurs. This file can be used for self-saving, alarm, or notification of the failed program. It can also help developers to obtain more system information, such as full Thread Dump or Core Dump) file.

Here is an example of the export thread transfer when 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 of the given Java Virtual Machine Process and save it in the D:/a.txt file.

Run the preceding code using the following parameters:

-Xmx20m-Xms5m "-XX: OnOutOfMemoryError = D:/tools/jdk1.7 _ 40/bin/printstack. bat % p "-XX: + HeapDumpOnOutOfMemoryError-XX: HeapDumpPath = d:/. dump

When the program unexpectedly exits, the System D will generate a new file a.txt, which stores the thread transfer information. In this example, the file path "D:/tools/jdk1.7 _ 40" is the author's JDK directory. you can replace it with your own JAVA_HOME directory and try it.

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.