티스토리 뷰

@GetMapping("/test")
public String test() {
	return "redirect:/test/"+Math.random();
}

@GetMapping("/test/{id}")
public String test(@PathVariable Double random) {
	...
}

스프링 낮은 버전을 사용 할 경우, 위와 같이 redirect prefix를 사용하여 string 형태로 redirect 할 경우 memory leak이 발생할 수 있다. spring 내부적으로 beanName을 기준으로 ConcurrentHashMap에 caching 하는 로직이 있어 pathVariable이 포함된 url 등을 return하게 될 경우 모든 redirect return url이 캐시되어 memory leak 발생 가능성이 생기는 것이다.

 

MAT로 OQL 조회하여 데이터 확인

 

해결방안

RedirectView나 ModelAndView를 명시적으로 만들어서 리턴해주면 된다. 또한 최신 버전에서는 "redirect:" 로만 캐싱하도록 수정되었다고한다.

 

참고

https://github.com/spring-projects/spring-boot/issues/13771

 

memory leak problem about redirect · Issue #13771 · spring-projects/spring-boot

our team has a memory leak problem lately. I have dump the heap and found that there are many strings in the heap. the strings is something about redirect string. So we reproduce the problem by the...

github.com

https://github.com/thymeleaf/thymeleaf-spring/issues/219

 

Thymeleaf's redirect view resolver + AspectJ leaks memory · Issue #219 · thymeleaf/thymeleaf-spring

This issue is very similar to the now fixed issue spring-projects/spring-framework#21583 . Andy Wilkinson: In short, the problem is that InternalResourceViewResolver calls AutowireCapableBeanFactor...

github.com

 

댓글