java: SavedModelBundle memory leak

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

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 36 (8 by maintainers)

Most upvoted comments

TF Core doesn’t need JavaCPP to allocate memory, it does that on its own.

Ok, we’ll see what they say in about the leaks valgrind found in the C API. Seems like the problem (or at least some of the problem) is there.