推荐链接:
总结——》【Java】
总结——》【Mysql】
总结——》【Spring】
总结——》【SpringBoot】
总结——》【MyBatis、MyBatis-Plus】
1、代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class TestController {
List<Object> list = new ArrayList<>();
@GetMapping("/test")
public String test() {
while (true) {
list.add(new Object());
}
}
}
2、配置
-Xmx20M -Xms20M
3、运行
http://localhost:8080/test
4、结果
Exception in thread "http-nio-8080-exec-2" java.lang.OutOfMemoryError: GC overhead limit exceeded
Spring Boot应用:内存溢出——Java无限循环与堆内存限制

本文探讨了一个Java程序在Spring Boot环境中,由于Controller中的无限循环导致堆内存溢出的问题。通过实例展示了如何使用`@GetMapping`注解创建控制器,配置-Xmx和-Xms限制,以及如何观察到`OutOfMemoryError: GC overhead limit exceeded`异常。关键在于理解内存管理并优化代码以避免此类问题。
2304

被折叠的 条评论
为什么被折叠?



