使用TensorFlow for Java进行内存泄漏 [英] Memory leak using TensorFlow for Java

查看:950
本文介绍了使用TensorFlow for Java进行内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下测试代码泄漏内存:

The following test code leaks memory:

private static final float[] X = new float[]{1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};

public void testTensorFlowMemory() {
    // create a graph and session
    try (Graph g = new Graph(); Session s = new Session(g)) {
        // create a placeholder x and a const for the dimension to do a cumulative sum along
        Output x = g.opBuilder("Placeholder", "x").setAttr("dtype", DataType.FLOAT).build().output(0);
        Output dims = g.opBuilder("Const", "dims").setAttr("dtype", DataType.INT32).setAttr("value", Tensor.create(0)).build().output(0);
        Output y = g.opBuilder("Cumsum", "y").addInput(x).addInput(dims).build().output(0);
        // loop a bunch to test memory usage
        for (int i=0; i<10000000; i++){
            // create a tensor from X
            Tensor tx = Tensor.create(X);
            // run the graph and fetch the resulting y tensor
            Tensor ty = s.runner().feed("x", tx).fetch("y").run().get(0);
            // close the tensors to release their resources
            tx.close();
            ty.close();
        }

        System.out.println("non-threaded test finished");
    }
}

有什么明显我做错了吗?基本流程是在该图形上创建图形和会话,创建占位符和常量,以便在以x为单位的张量上执行累积和。运行生成的y操作后,我关闭x和y张量以释放它们的内存资源。

Is there something obvious I'm doing wrong? The basic flow is to create a graph and a session on that graph, create a placeholder and a constant in order to do a cumulative sum on a tensor fed in as x. After running the resulting y operation, I close both the x and y tensors to free their memory resources.

我相信目前为止提供的帮助:

Things I believe so far to help:


  • 这不是Java对象内存问题。根据jvisualvm,堆不会增长,JVM中的其他内存不会增长。根据Java的本机内存跟踪,似乎不是JVM内存泄漏。

  • 关闭操作有所帮助,如果它们不在那里,内存会突飞猛进。有了它们,它仍然会变得非常快,但几乎与没有它们一样多。

  • cumsum运算符并不重要,它也适用于sum和其他运算符

  • 它发生在带有TF 1.1的Mac OS上,带有TF 1.1和1.2_rc0的CentOS 7

  • 注释掉 Tensor ty lines删除泄漏,因此它似乎在那里。

  • This is not a Java objects memory problem. The heap does not grow, other memory in the JVM is not growing- according to jvisualvm. Doesn't appear to be a JVM memory leak according to Java's Native Memory Tracking.
  • The close operations are helping, if they're not there the memory grows by leaps and bounds. With them in place it still grows pretty fast, but nearly as much as without them.
  • The cumsum operator is not important, it happens with sum and other operators as well
  • It happens on Mac OS with TF 1.1, and CentOS 7 with TF 1.1 and 1.2_rc0
  • Commenting out the Tensor ty lines removes the leak, so it appears to be in there.

有什么想法吗?谢谢!此外,这是一个演示此问题的Github项目,同时进行线程测试(以更快地增加内存)和一个没有线程的测试(表明它不是由于线程)。它使用maven并且可以简单地运行:

Any ideas? Thanks! Also, here's a Github project that demonstrates this issue with both a threaded test (to grow the memory faster) and an unthreaded test (to show it's not due to threading). It uses maven and can be run with simple:

mvn test


推荐答案

我相信确实有泄漏(特别是缺少 TF_DeleteStatus 对应于在JNI代码中分配)(感谢您提供重现的详细说明)

I believe there is indeed a leak (in particular a missing TF_DeleteStatus corresponding to the allocation in JNI code) (Thanks for the detailed instructions to reproduce)

我建议您在 http://github.com/tensorflow/tensorflow/issues
并希望它应该在最终1.2之前修复发布。

I'd encourage you to file an issue at http://github.com/tensorflow/tensorflow/issues and hopefully it should be fixed before the final 1.2 release.

(相关地,自从<$ c创建的 Tensor 对象以来,你在循环外部也有泄漏$ c> Tensor.create(0)未被关闭)

(Relatedly, you also have a leak outside the loop since the Tensor object created by Tensor.create(0) is not being closed)

UPDATE :这是固定的,1.2 .0-rc1应该不再有这个p roblem。

UPDATE: This was fixed and 1.2.0-rc1 should no longer have this problem.

这篇关于使用TensorFlow for Java进行内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆