手動寫java OOM記憶體溢位 java.lang.OutOfMemoryError: GC overhead limit exceeded

hcmony發表於2018-09-04
package com.hcmony.test.memory;

import java.io.Serializable;

/**
 * <h3>Shenjue.java基本描述</h3>
 * <p></p>
 *
 * @author hcmony
 * @since V1.0.0, 2018/09/04 14:43
 */
public class User implements Serializable{

	private static final long serialVersionUID = 353900218049881029L;

	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public User(int id, String name) {
		this.id = id;
		this.name = name;
	}
}
package com.hcmony.test.memory;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <h3>Shenjue.java基本描述</h3>
 * <p>
 *    java.lang.OutOfMemoryError:GCoverheadlimitexceeded
 *  【解釋】:JDK6新增錯誤型別,當GC為釋放很小空間佔用大量時間時丟擲;一般是因為堆太小,導致異常的原因,沒有足夠的記憶體。
 *  【解決方案】:
 *  1、檢視系統是否有使用大記憶體的程式碼或死迴圈;
 *  2、通過新增JVM配置,來限制使用記憶體:
 *  -XX:-UseGCOverheadLimit
 * </p>
 *
 * @author hcmony
 * @since V1.0.0, 2018/09/04 14:42
 */
public class GcMemory {

	private static List<User> userList = new ArrayList<User>();

	public static void main(String[] args) {
		while (true){
			int i = 0 ;
			User user = new User(i++, UUID.randomUUID().toString());
			userList.add(user);
		}
	}
}

結果如下:

Connected to the target VM, address: '127.0.0.1:58216', transport: 'socket'
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:201)
	at java.lang.String.substring(String.java:1921)
	at java.util.UUID.digits(UUID.java:386)
	at java.util.UUID.toString(UUID.java:376)
	at com.hcmony.test.memory.GcMemory.main(GcMemory.java:21)
Disconnected from the target VM, address: '127.0.0.1:58216', transport: 'socket'

Process finished with exit code 1

 

相關文章