tensorflow / tensorflow/java

SavedModelBundle memory leak

Open
#304 36 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
928
Forks
227
PR merge metrics
No merged PRs in 30d

Description

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();
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in App.main, focusing on the repeated SavedModelBundle.load(modelPath, "serve") and model.close() calls, and use the saved_model resource named in the issue. Confirm that memory continues growing across iterations and that the loading and closing path can complete repeatedly without an OutOfMemoryError.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.