如下有一段代码
package com.lm.demo.arthas.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;@RestController
@RequestMapping("/test")
public class TestController {@GetMapping("/test")public String test() throws InterruptedException {tst ();return "ok";}void tst () throws InterruptedException {long i =0;ArrayList<HeapTest> heapTests = new ArrayList<>();while (true){heapTests.add(new HeapTest());Thread.sleep(100);System.out.println(++i);}}
}
很明显,因为方法不可能执行力完,它的局部变量heapTests是不会释放的,但又在不停的new,它后面的一个个的new肯定也不会释放,最后肯定会OOM。
CMD--》jvisualvm