Skip to content

Show-Demo/memory-troubleshooting-springboot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memory-troubleshooting-springboot

Java内存问题排查demo

  • 内存泄漏:访问 http://localhost:8080/memory-leak 来模拟可控的泄漏增长。
    支持参数:iterationskbPerObjectlogEvery

  • 泄漏状态查看:访问 http://localhost:8080/memory-leak/stats

  • 内存溢出:访问 http://localhost:8080/out-of-memory 来模拟堆内存溢出。
    支持参数:mbPerChunklogEvery

  • 内存溢出(真实崩溃路径):访问 http://localhost:8080/out-of-memory-crash

启动方式

mvn clean package

JDK 11+:

java \
	-Xms64m \
	-Xmx64m \
	-Xlog:gc* \
	-XX:+HeapDumpOnOutOfMemoryError \
	-XX:HeapDumpPath=./heap.hprof \
	-jar target/memory-troubleshooting-springboot-0.0.1-SNAPSHOT.jar

JDK 8:

java \
	-Xms64m \
	-Xmx64m \
	-XX:+PrintGCDetails \
	-XX:+HeapDumpOnOutOfMemoryError \
	-XX:HeapDumpPath=./heap.hprof \
	-jar target/memory-troubleshooting-springboot-0.0.1-SNAPSHOT.jar

复现示例

# 触发泄漏增长(建议先小流量;每次请求增长约 iterations * kbPerObject)
curl "http://localhost:8080/memory-leak?iterations=800&kbPerObject=16&logEvery=100"

# 逐步加压(观察 GC 与响应时间)
curl "http://localhost:8080/memory-leak?iterations=1200&kbPerObject=16&logEvery=100"

# 清理泄漏容器(不重启进程)
curl "http://localhost:8080/memory-leak/clear"

# 查看当前泄漏状态(对象数、已用堆、最大堆)
curl "http://localhost:8080/memory-leak/stats"

# 触发堆 OOM
curl "http://localhost:8080/out-of-memory?mbPerChunk=4&logEvery=100"

# 触发堆 OOM(不捕获错误,进程直接异常退出)
curl "http://localhost:8080/out-of-memory-crash?mbPerChunk=4&logEvery=100"

说明:如果你把 JVM 堆设为 -Xmx64miterations=10000&kbPerObject=32 单次请求理论增量约 312MB,会很快把服务打挂。

About

Java内存泄漏和内存溢出演示demo

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages