---
title: "JVM内存区溢出场景模拟"
author: "ldz0414"
date: "2014-05-15T09:38:59Z"
tags: []
categories: ["编程语言"]
slug: "jvm"
draft: false
lastmod: "2014-05-15T09:38:59Z"
id: 2066621
url: "https://0414.iteye.com/blog/2066621"
---

模拟jvm内存溢出，需手动设置jvm启动参数。方法如下图：
在Default VM Arguments 配置参数：-Xms6m -Xmx6m -XX:PermSize=1m -XX:MaxPermSize=2m

![](http://dl2.iteye.com/upload/attachment/0097/1336/9d472de5-dac3-3e63-b9f8-19f97a618c5e.png)



java.lang.OutOfMemoryError: Java heap space
堆是用来存储对象的，当然对象不一定都存在堆里（由于逃逸技术的发展）。那么堆如果溢出了，一定是不能被杀掉的对象太多了。模拟 Heap 内存溢出，只要不断创建对象并保持有引用存在即可。

```java

package test;

import java.util.ArrayList;
import java.util.List;

public class TestOutOfMemoryHeapspace {
	public static void main(String[] args) {
		List list = new ArrayList();
		for(int i = 0; i < Integer.MAX_VALUE; i++) {
			TestMain tm = new TestMain();
			list.add(tm);
			System.out.println(tm);
		}
	}
}

```


```java

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:2760)
	at java.util.Arrays.copyOf(Arrays.java:2734)
	at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
	at java.util.ArrayList.add(ArrayList.java:351)
	at test.TestOutOfMemoryHeapspace.main(TestOutOfMemoryHeapspace.java:11)

```


java.lang.OutOfMemoryError: PermGen space
在运行时产生大量常量就可以实现让 Method Area 溢出的目的。运行是常量可以用 String 类的 intern 方法，不断地产生新的常量。

```java

package test;

import java.util.ArrayList;
import java.util.List;

public class TestOutOfMemoryPermGenspace {
	public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        int i = 0;
        while (true) {
            list.add(String.valueOf(i++).intern());
        }
    }
}

```


```java

Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
	at java.lang.String.intern(Native Method)
	at test.TestOutOfMemoryPermGenspace.main(TestOutOfMemoryPermGenspace.java:11)

```


java.lang.StackOverflowError
什么时候会让 Java Method Stack 栈溢出啊？栈的基本特点就是 FILO（First In Last Out），如果 in 的太多而 out 的太少，就好 overflow 了。而 Java Method Stack 的功能就是保存每一次函数调用时的“现场”，即为入栈，函数返回就对应着出栈，所以函数调用的深度越大，栈就变得越大，足够大的时候就会溢出。所以模拟 Java Method Stack 溢出，只要不断递归调用某一函数就可以。

```java

package test;

public class TestOutOfMemoryStackOverFlow {

	private int stackLength = 0;

	public void stackOverflow() {
		++stackLength;
		stackOverflow();
	}

	public static void main(String[] args) throws Throwable {
		TestOutOfMemoryStackOverFlow test = new TestOutOfMemoryStackOverFlow();

		try {
			test.stackOverflow();
		} catch (Throwable e) {
			System.out.println("stack length: " + test.stackLength);
			throw e;
		}
	}

}

```


```java

stack length: 9560
Exception in thread "main" java.lang.StackOverflowError
	at test.TestOutOfMemoryStackOverFlow.stackOverflow(TestOutOfMemoryStackOverFlow.java:9)
	at test.TestOutOfMemoryStackOverFlow.stackOverflow(TestOutOfMemoryStackOverFlow.java:9)
	at test.TestOutOfMemoryStackOverFlow.stackOverflow(TestOutOfMemoryStackOverFlow.java:9)
	at test.TestOutOfMemoryStackOverFlow.stackOverflow(TestOutOfMemoryStackOverFlow.java:9)
	at test.TestOutOfMemoryStackOverFlow.stackOverflow(TestOutOfMemoryStackOverFlow.java:9)

```