I would like to understand whether I should clean prototype-beans from memory manually by myself.
In the Spring documentation you can see: "The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding."
So from this it seems that you should clean prototype-beans by yourself.
However.
I'm using VisualVM memory profiler. I have created a number of prototype-beans. You can see 51 instances of them.
Then you can see the situation when the garbage collector clean the memory - all prototype-beans were cleared.
So can anyone clarify the situation? Are prototype-beans cleared successfully by garbage collector or we should clear them manually (if yes, how)?
Addition. Some guys asked to show the code of creating the prototype-beans. Actually I don't see any sense in this because in the particular example I am creating them only as a test and this don't concern the actual situation with cleaning prototype-beans from memory. Developers can create prototype-beans by different ways but the future behaviour of them doesn't or shouldn't depend on a creation method.
In our real project where we have more then 400 components annotated as Prototype-beans I can see the same behavior. I have made a number of requests to our system, see a number of created prototype beans through VisualVM, then after garbage collector clean the memory all prototype beans become cleared.
I show the testing code just in the hope that those who asked about this will give some senseful information about the real behavior not just empty words about this particular test bean creation.
Test creating of the prototype-beans for the example:
for(int i=0;i<10;i++){
Prototype1 prototype1=applicationContext.getBean(Prototype1.class);
Prototype2 prototype2=applicationContext.getBean(Prototype2.class);
prototype1.test1();
prototype2.test2();
}
Test example of the prototype-bean:
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class Prototype1 {
private int number;
public Prototype1() {
System.out.println("Prototype1 was created.");
}
public void test1(){
}
}