Home  >  Article  >  Java  >  Java memory overflow examples (heap overflow, stack overflow)

Java memory overflow examples (heap overflow, stack overflow)

高洛峰
高洛峰Original
2017-01-24 14:58:201395browse

Heap overflow:

/**
* @author LXA
* 堆溢出
*/
public class Heap
{
    public static void main(String[] args)
    {
        ArrayList list=new ArrayList();
        while(true)
        {
            list.add(new Heap());
        }
    }
}

Error report:

java.lang.OutOfMemoryError: Java heap space

Stack overflow:

/**
* @author LXA
* 栈溢出
*/
public class Stack
{
    public static void main(String[] args)
    {
        new Stack().test();
    }
    public void test()
    {
        test();
    }
}

Error report:

java.lang.StackOverflowError

For more java memory overflow examples (heap overflow, stack overflow) related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn