tensorflow / tensorflow/java

SavedModelBundle memory leak

未关闭
#304 36 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Java
星标
928
派生
227
PR 合并指标
30 天内没有已合并 PR

描述

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 18.04.5 LTS
  • TensorFlow installed from (source or binary): Included in java artifact
  • TensorFlow version: 2.4.1

Describe the current behavior
Loading and closing SavedModelBundle in an infinite loop leads to OutOfMemoryError:

Exception in thread "main" java.lang.OutOfMemoryError: Physical memory usage is too high: physicalBytes (7828M) > maxPhysicalBytes (7828M)
	at org.bytedeco.javacpp.Pointer.deallocator(Pointer.java:695)
	at org.tensorflow.internal.c_api.AbstractTF_Status.newStatus(AbstractTF_Status.java:70)
	at org.tensorflow.SavedModelBundle.load(SavedModelBundle.java:401)
	at org.tensorflow.SavedModelBundle.access$000(SavedModelBundle.java:59)
	at org.tensorflow.SavedModelBundle$Loader.load(SavedModelBundle.java:68)
	at org.tensorflow.SavedModelBundle.load(SavedModelBundle.java:242)
	at com.mycompany.app.App.main(App.java:13)

I use 0.3.1 version of the library. It is the only dependency:

<dependency>
  <groupId>org.tensorflow</groupId>
  <artifactId>tensorflow-core-platform</artifactId>
  <version>0.3.1</version>
</dependency>

The memory leak is reproducible with a model from this repository:
https://github.com/tensorflow/java/tree/master/tensorflow-core/tensorflow-core-api/src/test/resources/saved_model

Describe the expected behavior

I assume that the following code should run endlessly

Code to reproduce the issue

package com.mycompany.app;

import org.tensorflow.SavedModelBundle;
import java.net.URISyntaxException;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws URISyntaxException {
        var modelPath = Paths.get(App.class.getResource("/saved_model").toURI()).toString();
        while (true) {
            var model = SavedModelBundle.load(modelPath, "serve");
            model.close();
        }
    }
}

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 App.main 中的复现开始,重点关注重复调用 SavedModelBundle.load(modelPath, "serve") 和 model.close(),并使用 issue 中提到的 saved_model 资源。确认内存在多次迭代过程中持续增长,并且加载和关闭路径可以反复完成而不会发生 OutOfMemoryError。

由索引模型根据 Issue 内容生成。

评估

技术栈
java, tensorflow
领域
machine-learning
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
28/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。