为什么 resourceResolver 对象需要关闭

huangapple go评论67阅读模式
英文:

Why resourceResolver object should be closed

问题

此处所述,我应该关闭从工厂获得的资源解析器。

> 调用close()方法非常重要,一旦不再使用资源解析器,以确保正确清理任何系统资源。

但我不明白为什么要这么做,因为为什么Java垃圾回收框架不会自动释放内存呢?
我知道如果一个对象有引用,它就不会被垃圾收集。
让我们看个例子:

public class MyServiceImpl {

    @Reference
    ResourceResolverFactory rrf;

    public void someFunction() {
        ResourceResolver resourceResolver = rrf.getServiceResourceResolver(Map object);
        resourceResolver.getResource(path);
    }

}

在上面的代码片段中,resourceResolver对象仅用于获取资源。在函数执行结束后,resourceResolver对象没有更多的引用存在。所以我不明白为什么Java垃圾回收器不能释放内存?它仍然持有什么引用?

我能想到的一个原因是,我知道ResourceResolverFactory类提供了单例对象,对于单例类,在程序结束之前对象是不可回收的,而在这段时间内,如果我们创建了成千上万个ResourceResolver对象,那么内存将在程序结束之前不会被释放。这是一个有效的原因吗?
在单例ResourceResolverFactory和ResourceResolver对象之间是否存在内存映射?

英文:

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?

答案1

得分: 0

ResourceResolver 接口抽象了跨多个来源访问资源的详细信息。根据底层实现,这可以是 JCR、捆绑资源或其他形式的存储。访问底层的“数据库”可能需要某种形式的身份验证。例如,通常情况下,在从 AEM 的存储库中读取资源时,每个资源解析器都会有一个基础 JCR 会话是打开的。当不再需要该会话时,需要终止此会话。总体而言,这不仅仅涉及到纯粹的垃圾回收/内存管理约束,还取决于底层实现,可能会涉及一些其他资源清理。ResourceProvider 上的 Javadoc 在这方面有更详细的解释。

虽然如此,这种行为是通过多个接口及其相应的实现类来实现的,这些类引入了一些与垃圾回收过程有关的有趣内容。

CommonResourceResolverFactoryImpl 类实现了所有资源解析器工厂(参见 ResourceResolverFactory)的共享功能,它是一个单例,持有弱引用的映射,指向打开的 ResourceResolver 实例。

这需要在特定的 ResourceResolver 实例关闭时进行清理

您可能会注意到,每个 ResourceResolver 实例都关联有一个 ResourceResolverControl。正是 ResourceResolverControl 类确保所有的 ResourceProvider 实例与包围的 ResourceResolver 一起被正确关闭。当您关闭特定的 ResourceResolver 时,这是由相关的 ResourceResolverFactory 协调的。【链接到源代码】

据我理解,这些类之间的关系以及它们的垃圾回收影响不是我们必须调用 ResourceResolver#close 的原因。设计反映了抽象出不同提供者的需要,其中一些提供者使用需要进行清理的资源。

英文:

The ResourceResolver interface abstracts away the details of accessing resources across multiple sources. Depending on the underlying implementation, this could be the JCR, a bundle resource or some other form of storage. Access to the underlying "database" may require some form of authentication. For example, typically, when reading resources from the repository in AEM, there's an underlying JCR session open for each Resource Resolver. This session needs to be terminated when it's no longer needed. Broadly speaking, there's more to it than pure garbage collection/memory management constraints and, depending on the underlying implementation, there may be some other resource cleanup involved. The Javadoc on ResourceProvider explains this in a little more detail.

That said, this behavior is achieved with a number of interfaces and their corresponding implementation classes that introduce a few interesting things with a bearing on the garbage collection process.

TheCommonResourceResolverFactoryImpl class, which implements the shared functionality of all resource resolver factories (see ResourceResolverFactory) is a Singleton that holds a map of weak references to open ResourceResolver instances.

This needs to be cleaned up as a particular ResourceResolver instance is closed.

You may notice that there's a ResourceResolverControl associated with each of those ResourceResolver instances. It is the ResourceResolverControl class that ensures all ResourceProvider instances are properly closed together with the encompassing ResourceResolver. As you close a particular ResourceResolver, this is orchestrated by the relevant ResourceResolverFactory

The way I understand it, the relationships between those classes and their garbage collection implications aren't the reason we have to call ResourceResolver#close. The design reflects the need to abstract away the different providers, some of which use resources calling for a cleanup.

huangapple
  • 本文由 发表于 2020年9月14日 23:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定