Java Heap Memory Overflow simulation

Source: Internet
Author: User

Let's look at the Java heap:

For the allocation of the Java memory area, you can view an article in the Java Runtime data region.

The Java heap is the largest chunk of virtual machine memory management, which is thread-shared, and all threads in a Java process can access the data in that zone. Most areas of the Java heap are stored instance objects, which we often call objects in Java object-oriented programming. For example, I created a class that, through the constructor of this class, new object, the new object is stored on the Java heap. It can be subdivided into the new generation and the old age according to the time of the object generation and the reference time.

The Java heap size is controlled by the-XMS and-XMX parameters when the virtual machine starts. If the instance object is not allocated on the Java heap and the heap cannot be extended, an OutOfMemoryError exception will be thrown.

Next, simulate the overflow of the Java heap. Here is the test code:

 Packagecom.woasis.jvm.classloading;Importjava.util.ArrayList;Importjava.util.List;/*** Simulated heap overflow *-xms20m minimum initial memory *-xmx20m Max Memory value *-xx:+heapdumponoutofmemoryerror*/ Public classDeapoutofmemory {/*** Inner class, used to create objects, stored in heap memory*/    Static classtestoom{} Public Static voidMain (string[] args) {//used to store mock objects, prevent GC recycling, and use list for object referenceslist<testoom> list =NewArraylist<>();  for (;;) {List.add (Newtestoom ()); }    }}

Facilitates simulation of overflow, and memory snapshots when crawling memory overflows, such as setting virtual machine parameters:

  -xms20m Java heap Memory initialization size is 20M

  -xmx20m Java heap Memory maximum value is 20M

  -xx:+heapdumponoutofmemoryerror Fetches a Java heap snapshot when an OutOfMemoryError exception occurs. where + is the meaning of the open.

  

Run the main method after setting the virtual machine memory parameters. In the case of a OutOfMemoryError exception, the Java Heap space is where it occurs.

  

Open the captured snapshot, as you can see that the number of internal class Testoom instance objects is 810326.

  

Java Heap Memory Overflow simulation

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.