从资产fodler加载~200mb文件时出现OutOfMemoryError

时间:2017-04-23 13:00:17

标签: java android android-ndk tensorflow

当我运行应用程序并想要加载

时,Android无法分配足够的内存
java.lang.OutOfMemoryError: Failed to allocate a 202728962 byte allocation with 14422192 free bytes and 171MB until OOM

此前我还在此处描述了一个错误:https://github.com/tensorflow/tensorflow/issues/9343

我想我有足够的可用内存(通常1.0-1.5 GB是免费的):

enter image description here

它只是java android限制吗?我可以用java以某种方式解决它吗?或者只能使用NDK吗?

完整错误代码:

04-21 09:40:52.731 31597-31597/org.tensorflow.demo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.tensorflow.demo, PID: 31597
    java.lang.OutOfMemoryError: Failed to allocate a 202728962 byte allocation with 14422192 free bytes and 171MB until OOM
        at org.tensorflow.contrib.android.TensorFlowInferenceInterface.loadGraph(TensorFlowInferenceInterface.java:377)
        at org.tensorflow.contrib.android.TensorFlowInferenceInterface.<init>(TensorFlowInferenceInterface.java:96)
        at org.tensorflow.demo.TensorFlowYoloDetector.create(TensorFlowYoloDetector.java:111)
        at org.tensorflow.demo.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:131)
        at org.tensorflow.demo.CameraActivity$1.onPreviewSizeChosen(CameraActivity.java:159)
        at org.tensorflow.demo.CameraConnectionFragment.setUpCameraOutputs(CameraConnectionFragment.java:421)
        at org.tensorflow.demo.CameraConnectionFragment.openCamera(CameraConnectionFragment.java:428)
        at org.tensorflow.demo.CameraConnectionFragment.access$000(CameraConnectionFragment.java:64)
        at org.tensorflow.demo.CameraConnectionFragment$1.onSurfaceTextureAvailable(CameraConnectionFragment.java:95)
        at android.view.TextureView.getHardwareLayer(TextureView.java:368)
        at android.view.View.updateDisplayListIfDirty(View.java:15175)
        at android.view.View.draw(View.java:15971)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
        at android.view.View.updateDisplayListIfDirty(View.java:15193)
        at android.view.View.draw(View.java:15971)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
        at android.view.View.draw(View.java:16204)
        at android.view.View.updateDisplayListIfDirty(View.java:15198)
        at android.view.View.draw(View.java:15971)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
        at android.view.View.updateDisplayListIfDirty(View.java:15193)
        at android.view.View.draw(View.java:15971)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
        at android.view.View.updateDisplayListIfDirty(View.java:15193)
        at android.view.View.draw(View.java:15971)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
        at android.view.View.draw(View.java:16204)
        at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
        at android.view.View.updateDisplayListIfDirty(View.java:15198)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:282)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:288)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:323)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2642)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2461)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2094)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6045)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
        at android.view.Choreographer.doCallbacks(Choreographer.java:672)
        at android.view.Choreographer.doFrame(Choreographer.java:608)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5441)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
04-21 09:40:52.740 1501-2367/? E/ActivityManager: Invalid thumbnail dimensions: 0x0
loadGraph类的

TensorFlowInferenceInterface方法:

private void loadGraph(InputStream var1, Graph var2) throws IOException {
        long var3 = System.currentTimeMillis();
        Trace.beginSection("initializeTensorFlow");
        Trace.beginSection("readGraphDef");
        byte[] var5 = new byte[var1.available()];
        int var6 = var1.read(var5);
        if(var6 != var5.length) {
            throw new IOException("read error: read only " + var6 + " of the graph, expected to read " + var5.length);
        } else {
            Trace.endSection();
            Trace.beginSection("importGraphDef");

            try {
                var2.importGraphDef(var5);
            } catch (IllegalArgumentException var9) {
                throw new IOException("Not a valid TensorFlow Graph serialization: " + var9.getMessage());
            }

            Trace.endSection();
            Trace.endSection();
            long var7 = System.currentTimeMillis();
            Log.i("TensorFlowInferenceInterface", "Model load took " + (var7 - var3) + "ms, TensorFlow version: " + TensorFlow.version());
        }
    }

P.S。演示应用程序链接:github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android

此外,我还可以尝试使用本机代码路径来加载文件:graphsgithub.com/tensorflow/tensorflow/issues/9343#issuecomment-295959477 但我对C ++ / NDK不太好(我只是不用它) 所以.cc文件可用于加载文件。如果有人帮助我将它集成到TensorFlow示例Android项目中,我将不胜感激。 但我仍然希望有没有NDK的解决方案,因为对于Android 2016-2017设备来说200mb真的是这么大吗?

1 个答案:

答案 0 :(得分:1)

  

我想我有足够的可用内存(通常1.0-1.5 GB是免费的)

那是设备。它不适用于您的应用

  

它只是java android限制吗?

这是一个Android运行时限制。你有一个堆限制,你的堆可能会碎片化。

  

我可以用java以某种方式解决它吗?

可能不是。您可以使用android:largeHeap请求大于平均值的堆,但是:

  • 在低端设备(例如,Android One)上,这可能不比现在的更大
  • 大堆可能不超过200MB
  • 即使大堆超过200MB,您也可能没有一个连续的200 MB空闲堆空间块
  • 如果你消耗大量内存,你的进程会在进入后台时迅速终止
  

或者只能使用NDK吗?

我不知道TensorFlow是否适用于NDK,因为我没有使用过TensorFlow。您可以通过NDK分配系统RAM。是否可以分配200MB的系统RAM取决于设备上的系统RAM量。

  

因为Android 2016-2017设备的200mb真的如此大吗?

200MB,就像单个连续内存块一样,对于Android 2016-2017设备来说是一个非常大的尺寸。设备的使用年限与其可用的系统RAM无关。今天制造的大量设备安装了1GB或更少的系统RAM,更不用说操作系统进程运行后的空闲内容了。例如,Android One设备可能附带512MB系统RAM。