0

We've been diagnosing an apparent memory leak in a Spring Boot application. We were unable to find anything that stood out in our code, but using VisualVM's memory profiler, we found something odd. 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.

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?

The following is the class' annotations.

@Slf4j
@Validated
@RestController
@RequestMapping("profile")
public class ProfileController {
    ...
}
Ben
  • 332
  • 3
  • 16
  • 2
    Spring controllers are not singletons. Please, see https://stackoverflow.com/questions/11508405/are-spring-mvc-controllers-singletons. – Mykhailo Hodovaniuk Feb 13 '20 at 17:51
  • The Garbage Conneltor only runs on demand (free memory is getting low), so if you're not profiling in production you might never see a gc run unless you create quite some load. If the gc runs and doesn't delete the expected objects use a profiler like JProfiler to find the references to the specific objects. – Nicktar Feb 13 '20 at 18:05
  • Can you share how you annotate or create your controllers? Since like said bellow controllers are Singleton and are intended to last during all the time your application is running. Have you some way to share the list of objects created after and before your requests is processed ? – EFOE Feb 13 '20 at 18:14
  • @EFOE I read that each request would create a new controller while troubleshooting this prior. My mistake. I guess the problem is the controllers being created repeatedly instead. – Ben Feb 13 '20 at 19:11
  • Can you provide how you instantiate your controllers? ( annotations or configurations class), If not please check that you don't have beans with scope ( request or session ) – EFOE Feb 14 '20 at 08:58

1 Answers1

0

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.

Ben
  • 332
  • 3
  • 16