On a training I heard several times that new
in controller or in service in Spring boot application is bad practice because it can cause a memory leak. And one told us to use Lombok
Lombok is great tool but I'm wondering about memory leaks with new
. Moreover, if I use delombok I see the same new
keyword there.
As far as I know garbage collector will delete all objects that are not referenced anymore. And this includes Spring boot controllers too. I don't speak about static variables, not closed streams, etc. Just a local variable in controller method.
Please tell me if I'm wrong and if I don't understand something correctly.
On a training I heard several times that new in controller or in service in Spring boot application is bad practice because it can cause a memory leak. And one told us to use Lombok
Creating objects in our spring beans with the new
operator and using Lombok are really two different things while the primary goal are different : creating an object for the first one versus generating code in the compiled class to reduce boiler plate code.
I don't speak about static variables, not closed streams, etc. Just a local variable in controller method.
About what you understood concerning the new
operator, you should just forget and understand that at each time you have to create an object that is not a bean managed by a container you have to use the new
operator.
For example how do you want to persist an entity in database without creating it ?
Besides local variables live the span of the method invocation. So no memory leak is likely but if you create millions of big objects in your method. But why the hell would you do that ?