Java memory area and simulated memory area exception

Source: Internet
Author: User

I drew a mind map of the memory area of Java and the main functions of each area.

simulating Java heap overflow

The Java heap is used to store object instances, as long as the objects are constantly created and the GC roots to the object to avoid being purged by the recycle mechanism, the Java heap overflow can be simulated.

 PackageHXL.INSIST.JVM;ImportJava.util.ArrayList;ImportJava.util.List;/** * Below is the JVM Args: * Minimum value of the-xms20m heap-xmx20m heap (set to the same to avoid heap auto-scaling) *-xx:+heapdumponoutofmemoryerror when a memory overflow exception occurs on a virtual machine, Dum P out the current heap dump snapshot *-xx:heapdumppath=e:\eclipseworkspace\understandingthejvm\hprof set the path to the generated heap dump snapshot * @author HANXL * * / Public  class heapoutofmemory {    StaticClass Stuffobject {}Staticlist<stuffobject> list =NewArraylist<stuffobject> (); Public Static void Main(string[] args) {Thread thread =NewThread (NewRunnable () { Public void Run() {createobj ();         }        });    Thread.Start (); }Private Static void Createobj() { while(true) {List.add (NewStuffobject ()); }    }}

Analyze heap dump snapshots with memoryanalyzer such as:

The shortest path from the root element to the memory consumption aggregation point can clearly see the entire chain of references.

In the above diagram, we can see clearly that this object collection holds a large number of references to the internal class Stuffobject object, which is the memory leak caused by it.

simulating Java Virtual machine stack Overflow

With respect to the virtual machine stack, two exceptions are regulated in the Java Virtual machine:

    1. A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the maximum allowed depth for the virtual machine.
    2. Throws a OutOfMemoryError exception if the virtual machine is not able to request enough memory space when the stack is extended.

Simulate the first case:

package hxl.insist.jvm;/** * 下面是JVM Args: * -Xss128k 设置栈容量大小 * @author hanxl */publicclass JavaVMStackSOF {    publicvoidstackLeak() {        stackLeak();    }    publicstaticvoidmainthrows Throwable {        new JavaVMStackSOF().stackLeak();    }}

Simulation of the second case:

 PackageHXL.INSIST.JVM;/** * Below is the JVM Args: *-xss2m Set Stack capacity * @author HANXL */ Public  class javavmstackoom {     Public Static void Main(string[] args) {NewJavavmstackoom (). Threadinvokemethod (); } Public void Threadinvokemethod() { while(true) {Thread thread =NewThread (NewRunnable () {@Override                 Public void Run() {infiniteloop ();            }            });        Thread.Start (); }    }Private void Infiniteloop() { while(true)              ; }}
Compare the two scenarios for how they are implemented differently?

The Java Virtual machine stack is thread-private, and its life is the same as the thread at the same time. We make a virtual machine stack than a box,-XSS is the size of the box, and a thread can only correspond to one box. Each Java method, when executed, creates a stack frame in the box to store some information such as a local variable table.

So in order to create the first case of the exception, we set the size of the box smaller, using recursion to constantly call the method, so as to burst the box, and in order to create the exception in the second case, we should set the size of the box smaller, more boxes, so that it can not request enough memory space.

As long as you know the memory area of the mind map above, it is also easy to simulate other memory areas anomalies.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java memory area and simulated memory area 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.