流导致内存泄漏

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

InputStream causing memory leaks

问题

以下是翻译好的内容:

我正在使用Restlet Framework,但我的问题更是关于Java中流(Streams)的一般性问题。我有以下代码,它在GET HTTP请求时返回一个“blob”数据/文件:

@Override
public Representation getBlob() {
   InputStream is = entityRepository.getEntityBlob(appId, namespace, entityType, entityId, blobName);
   Representation representation = new InputRepresentation(is);
   representation.setMediaType(MediaType.APPLICATION_OCTET_STREAM);
   representation.setSize(count);
   return representation;
}

我还在使用k6.io进行负载测试,以测试这个资源。我已经为1000个活动连接测试了10分钟,没有任何问题。但是,当在同一台机器上测试更长的时间,比如几个小时,它会开始占用机器的所有内存,最终导致机器停止响应。

似乎出现了一个“泄漏”问题,但我无法确定,因为实际上没有任何错误。

更新:

代码如下所示:

public class BlobHashServerResource extends BaseServerResource implements BlobHashResource {
   @Override
   public Representation getBlob() { 
      //... 
   }
}

这个BlobHashServerResource映射到:

public class BackendApplication extends Application {
  @Override
  public Restlet createInboundRoot() {
     router.attach("blobs/{blobHash}", BlobHashServerResource.class);
     return router; 
  }
}

web.xml:

  <servlet>
    <servlet-name>RestletServlet</servlet-name>
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
      <param-name>org.restlet.application</param-name>
      <param-value>com.mycompany.BackendApplication </param-value>
    </init-param>
  </servlet>

请注意,我只翻译了代码和相关的描述,其他部分被忽略了。

英文:

I'm using Restlet Framework but my question is more general Java question on Streams. I have this code that returns a "blob" data / file on GET HTTP request:

@Override
public Representation getBlob() {
   InputStream is = entityRepository.getEntityBlob(appId, namespace, entityType, entityId, blobName);
   Representation representation = new InputRepresentation(is);
   representation.setMediaType(MediaType.APPLICATION_OCTET_STREAM);
   representation.setSize(count);
   return representation;
}

I am also using k6.io load testing to test this resource, tested this for 1000 active connections for 10 mins and it did not have any issue, but this same machine and resource when tested for more time, hours for example, it then turns it starts eating all the memory of the machine causing it to halt eventually.

There seems to be a "leak" here but I cannot pinpoint as there is really no error at all.

Update:

This is how it looks:

public class BlobHashServerResource extends BaseServerResource implements BlobHashResource {
   @Override
   public Representation getBlob() { 
      //... 
   }
}

This BlobHashServerResource maps to

public class BackendApplication extends Application {
  @Override
  public Restlet createInboundRoot() {
     router.attach(&quot;blobs/{blobHash}&quot;, BlobHashServerResource.class);
     return router; 
  }
}

web.xml

  &lt;servlet&gt;
    &lt;servlet-name&gt;RestletServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.restlet.ext.servlet.ServerServlet&lt;/servlet-class&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;org.restlet.application&lt;/param-name&gt;
      &lt;param-value&gt;com.mycompany.BackendApplication &lt;/param-value&gt;
    &lt;/init-param&gt;
  &lt;/servlet&gt;

答案1

得分: 0

这个问题的解决方法是更新Restlet Framework版本,因为这是旧版Restlet中已知的InputStream问题/bug,然而一些Restlet软件包没有2.4.0版本,所以我在这里还指出了经过测试的兼容版本:

    <properties>
       <version.restlet>2.4.0</version.restlet>
    </properties>
   
    <dependencies>
       <!-- Restlet -->
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${version.restlet}</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.fileupload</artifactId>
            <version>${version.restlet}</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.servlet</artifactId>
            <version>${version.restlet}</version>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.swagger</artifactId>
            <version>${version.restlet}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.raml</groupId>
                    <artifactId>raml-parser</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.xstream</artifactId>
            <!--version>${version.restlet}</version-->
            <version>2.3.12</version>
            <exclusions>
                <exclusion>
                    <groupId>org.codehaus.jettison</groupId>
                    <artifactId>jettison</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.thoughtworks.xstream</groupId>
                    <artifactId>xstream</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.apispark</artifactId>
            <version>2.3.12</version>
            <exclusions>
                <exclusion>
                    <groupId>org.raml</groupId>
                    <artifactId>raml-parser</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.restlet.jee</groupId>
            <artifactId>org.restlet.ext.json</artifactId>
            <version>${version.restlet}</version>
        </dependency>
    </dependencies>
英文:

The solution for this is to update Restlet Framework version, as this is a known InputStream issue/bug for older Restlet version, however some Restlet packages don't have the 2.4.0 version so I also indicated here the tested compatible version:

    &lt;properties&gt;
&lt;version.restlet&gt;2.4.0&lt;/version.restlet&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;!-- Restlet --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.restlet.jee&lt;/groupId&gt;
&lt;artifactId&gt;org.restlet&lt;/artifactId&gt;
&lt;version&gt;${version.restlet}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.restlet.jee&lt;/groupId&gt;
&lt;artifactId&gt;org.restlet.ext.fileupload&lt;/artifactId&gt;
&lt;version&gt;${version.restlet}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.restlet.jee&lt;/groupId&gt;
&lt;artifactId&gt;org.restlet.ext.servlet&lt;/artifactId&gt;
&lt;version&gt;${version.restlet}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.restlet.jee&lt;/groupId&gt;
&lt;artifactId&gt;org.restlet.ext.swagger&lt;/artifactId&gt;
&lt;version&gt;${version.restlet}&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.raml&lt;/groupId&gt;
&lt;artifactId&gt;raml-parser&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.restlet.jee&lt;/groupId&gt;
&lt;artifactId&gt;org.restlet.ext.xstream&lt;/artifactId&gt;
&lt;!--version&gt;${version.restlet}&lt;/version--&gt;
&lt;version&gt;2.3.12&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.codehaus.jettison&lt;/groupId&gt;
&lt;artifactId&gt;jettison&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;exclusion&gt;
&lt;groupId&gt;com.thoughtworks.xstream&lt;/groupId&gt;
&lt;artifactId&gt;xstream&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.restlet.jee&lt;/groupId&gt;
&lt;artifactId&gt;org.restlet.ext.apispark&lt;/artifactId&gt;
&lt;version&gt;2.3.12&lt;/version&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.raml&lt;/groupId&gt;
&lt;artifactId&gt;raml-parser&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.restlet.jee&lt;/groupId&gt;
&lt;artifactId&gt;org.restlet.ext.json&lt;/artifactId&gt;
&lt;version&gt;${version.restlet}&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;

huangapple
  • 本文由 发表于 2020年10月4日 19:47:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64194215.html
匿名

发表评论

匿名网友

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

确定