简体   繁体   English

Spring Controller 不被视为单例

[英]Spring Controller not treated as singleton

We've been diagnosing an apparent memory leak in a Spring Boot application.我们一直在诊断 Spring Boot 应用程序中明显的内存泄漏。 We were unable to find anything that stood out in our code, but using VisualVM's memory profiler, we found something odd.我们无法在我们的代码中找到任何突出的东西,但是使用 VisualVM 的内存分析器,我们发现了一些奇怪的东西。 When a controller is being hit by a request, a new instance of that controller is created.当控制器被请求击中时,会创建该控制器的一个新实例。 As specified below, these controllers should be singletons.如下所述,这些控制器应该是单例的。 Furthermore, these superfluous instances are never garbage collected and keep piling up.此外,这些多余的实例永远不会被垃圾收集并不断堆积。 These controllers aren't referenced anywhere in the code, and are only created by whatever Spring magic creates it in the first place.这些控制器在代码中的任何地方都没有被引用,并且只由首先创建它的 Spring 魔法创建。

So my question is: what can cause a Spring component like this to start behaving like this, and how would I potentially go about debugging it?所以我的问题是:什么会导致这样的 Spring 组件开始像这样运行,我将如何调试它?

The following is the class' annotations.以下是类的注释。

@Slf4j
@Validated
@RestController
@RequestMapping("profile")
public class ProfileController {
    ...
}

The problem was intermittent.问题是间歇性的。 After restarting the the computer it ran on, the problem disappeared.重新启动它运行的计算机后,问题消失了。

For those wondering, the source of the problem turned out to be a thread leak.对于那些想知道的人来说,问题的根源原来是线程泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM