tensorflow / tensorflow/java

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

オープン
#371 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。