Common error in Java Virtual machine

Source: Internet
Author: User
Tags throwable

1. The simplest memory overflow

public class Heapoom {    heapoom[] testlist= new heapoom[100000];    public static void Main (string[] args) {        list

  

2, virtual machine stack and local method stack Overflow (-XSS: stack memory capacity)

Two kinds of exceptions:

A Stackoverflowerror exception is thrown if the thread requests a depth greater than the maximum allowed depth for the virtual machine.

Throws a OutOfMemoryError exception if the virtual machine cannot request enough memory space on the scale stack.

Single Thread time stack overflow:

public class StackOverflow {    private int stacklength = 1;    public void Stackleak () {        stacklength++;        Stackleak ();    }    public static void Main (string[] args) throws Throwable {        StackOverflow stackoverflowerror = new StackOverflow (); 
   
    try {            stackoverflowerror.stackleak ();        } catch (Throwable e) {            System.out.println ("Stack depth:" +stackoverflowerror.stacklength);            throw e;}}    }
   

Stack overflow under multi-threading:

A memory overflow exception can be generated by constantly creating threads. In this case, the larger the memory allocated for each thread's stack, the more likely it is to produce a memory overflow exception.

The reason is that there is a limit to the memory assigned to each process by the operating system, such as 32-bit Windows limited to 2GB. The virtual machine provides parameters to control the maximum value of both the Java heap and the two-part memory of the method area. The remaining memory is 2GB minus XMX (maximum heap capacity), minus maxpermsize (maximum method area capacity)

public class Stackoverthread {    private void Donnotstop () {        while (true) {            donnotstop ()        }    }    public void Stackleakbythread () {if        (true) {            thread thread = new Thread (new Runnable () {public                void run () {                    donnotstop ();                }            });            Thread.Start ();        }    }    public static void Main (string[] args) {        Stackoverthread stackoverthread = new Stackoverthread ();        Stackoverthread.stackleakbythread ();    }}

  

3.

  

Common error in Java Virtual machine

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.