As stated here, I should close the resource resolver obtained from factory.
It is very important to call the close() method once the resource resolver is not used any more to ensure any system resources are properly clean up.
But I could not understand why should I do it because why java garbage collection framework does not release the memory automatically? I know an object will not be garbage collected if it has a reference. Lets take this example:
public class MyServiceImpl{
@Reference
ResourceResolverFactory rrf;
public void someFunction(){
ResourceResolver resourceResolver = rrf.getServiceResourceResolver(Map object);
resourceResolver.getResource(path);
}
}
In the above piece of code, resourceResolver object is used to just to get the resource. And after the end of function execution there is no more reference present for resourceResolver object. So I do not understand why memory cannot be free by the java garbage collector? What reference it still holds?
One reason I can think of, as I know the ResourceResolverFactory class gives Singleton object and in case of Singleton class the object is not eligible for garbage collection until the program ends and during this time if we create thousands of ResourceResolver objects then memory will not free until program ends. Is this a valid reason ? Is there any memory mapping between Singleton ResourceResolverFactory and ResourceResolver object?