tensorflow / tensorflow/java

Memory leak of TString.tensorOf(Shape shape, DataBuffer<String> data)

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

还没有人认领这个 Issue。

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

描述

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

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 x86_64): macOS 11.5.2
  • TensorFlow installed from (source or binary): Binary
  • TensorFlow version (use command below): 2.5
  • Java version (i.e., the output of java -version): Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
  • Java command line flags (e.g., GC parameters): -ea
  • Python version (if transferring a model trained in Python): 3.7
  • Bazel version (if compiling from source): N/A
  • GCC/Compiler version (if compiling from source): N/A
  • CUDA/cuDNN version: N/A
  • GPU model and memory: N/A
  • Tensorflow Java version: 0.3.2

Describe the current behavior
When I create and destroy TString instances repeatedly in a for loop, memory usage grows. In my production environment, where a TF model is served by a java service, it would cause memory usage alert after processing certain amount of requests.

Describe the expected behavior
Memory usage should not grow since created TString instances are closed in each iteration.

Code to reproduce the issue

    @Test
    void testPerformance() throws Exception {
        // Some code to load a model but never used it in this test case. Otherwise, the following code would exit randomly. See https://github.com/tensorflow/java/issues/370 .
        final String[] result = new String[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "c", "c", "c"};
        for (int i = 0; i < 65536; ++i) {
            try (final TString tstring = TString.tensorOf(Shape.of(1, result.length), DataBuffers.ofObjects(result))) {
               // this block is empty.
            }
        }
  }

Other info / logs
4 iterations
image
8192 iterations
image
65536 iterations
image

The above are IntelliJ IDEA's memory profiling screenshots. The differences are as following:

  • The first was recorded when the for loop runs 4 iterations (; i < 4; ...). The TString.tensorOf calls only use 2% memory of its parent call.
  • The second wass recorded when the for loop runs 8192 iterations (; i < 8192; ...). The TString.tensorOf calls use 49.05% memory of its parent call.
  • The third was recorded when the for loop runs 65536 iterations (; i < 65536; ...). The TString.tensorOf calls use up to 83.56% memory of its parent call.

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先运行围绕 TString.tensorOf(Shape, DataBuffer) 和 DataBuffers.ofObjects(result) 提供的 Java 复现代码,然后检查 TString.close() 如何释放资源。比较迭代 4 次、8192 次和 65536 次这三种情况下的内存行为。当反复创建并关闭 TString 实例不再导致内存使用量意外增长时,即可视为完成。

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

评估

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

把新 issue 发到你的邮箱

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