Realize jvm memory overflow

So how do we build a heap memory overflow? In fact, it is very simple, we only need to define an Listobject, and then keep stuffing objects Listinto it . Because as long as the Controller is not recycled, the member variables in it will not be recycled either. This will lead to more and more objects in the List, which will take up more and more memory, and finally burst our memory.

Create a User object

Here we first create a User object.

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	private int id;
	private String name;
	
}

Create a Controller object

Next, let's create a Controller to continuously stuff objects into the List collection.

@RestController
public class MemoryController {
	
	private List<User>  userList = new ArrayList<User>();
	
	/**
	 * -Xmx32M -Xms32M
	 * */
	@GetMapping("/heap")
	public String heap() {
		int i=0;
		while(true) {
			userList.add(new User(i++, UUID.randomUUID().toString()));
		}
	}
	
}

Configure the size of the jvm's memory when the project starts

In order to achieve our effect faster, we configure the size of jvm memory when the springBoot project starts.

-Xms30m -Xmx32m

Theoretical knowledge:
1. The default heap space size is determined according to the memory size of the physical machine.
Assuming that the physical machine has 16G memory, the default heap space size of the virtual machine is as follows:

Minimum value: 16 / 64 = 0.25G * 1024 = 256 m

Maximum: 16 / 4 = 4G 

2. The above is only a "theoretical value", and the "actual value" will be smaller than the "theoretical value".
Heap space size setting (multiple pictures):
as shown in the figure: [Services] > [****SpringbootApplication] > [right mouse button] > [Edit Configuration... Or shortcut key Shift+F4]

  Mouse click [ Environment ] Or shortcut key Alt+M

 【VM options:】>

-Xms30m -Xmx32m

 Some people suggest that the two values ​​are equal , but I don't think it's okay. It depends on your personal preference .

 Whether the verification is valid or not:

        System.out.println("-------------======= 华丽的分割线 =========----------");
        //返回Java虚拟机中的堆内存总量
        long xmsMemory = Runtime.getRuntime().totalMemory() / 1024 / 1024;
        //返回Java虚拟机中使用的最大堆内存
        long xmxMemory = Runtime.getRuntime().maxMemory() / 1024 / 1024;
        System.out.println("-Xms:" + xmsMemory + "M");
        System.out.println("-Xmx:" + xmxMemory + "M");
//        System.out.println("系统内存大小为:" + xmsMemory * 64.0 / 1024 + "G");
//        System.out.println("系统内存大小为:" + xmxMemory * 4.0 / 1024 + "G");
//        在使用 -Xms500m -Xmx500m -XX:+PrintGCDetails需要把等待时间代码注释掉
//        try {
//            Thread.sleep(1000000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

Where to post the code depends on your preference, I will paste it directly in the startup class

 Start the project to see the effect

 When accessing the interface, a memory overflow error occurs

Exception in thread "http-nio-8081-Poller" java.lang.OutOfMemoryError: GC overhead limit exceeded                      

 

 

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/131154716