Outofmemoryerror/OOM/memory overflow exception instance analysis-Heap Memory exception

Source: Internet
Author: User

Java heap memory overflow

As long as you constantly create objects and ensure that there is an accessible path between the GC roots and objects to avoid the garbage collection mechanism clearing these objects,

The memory overflow exception occurs when the number of objects reaches the maximum heap capacity limit. The Code is as follows:

import java.util.ArrayList;import java.util.List;public class HeapOOM {    static class OOMObject {    }    public static void main(String[] args) {        List<OOMObject> list = new ArrayList<>();        while (true) {            list.add(new OOMObject());        }    }}

During running, you need to set the Java heap size, which cannot be expanded. The operation is as follows:

Then fill in the JVM startup parameter-xms20m-xmx20m-XX: + heapdumponoutofmemoryerror

The size of the Java heap is limited to 20 mb. the maximum and minimum values are the same and cannot be extended. The-XX: + heapdumponoutofmemoryerror parameter allows the virtual machine to appear

When a memory overflow exception occurs, dump the current memory dump snapshot for later analysis. (If you are interested, you can use the memory image analysis tool eclipse memory analyzer to open the heap dump snapshot file for analysis)

The running result is as follows:

 

Outofmemoryerror/OOM/memory overflow exception instance analysis-Heap Memory exception

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.