当我运行应用程序并想要加载时,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](https://istack.dev59.com/jVFRp.webp)
完整的错误代码:
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
TensorFlowInferenceInterface
类的 loadGraph
方法:
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());
}
}
附带演示应用程序链接:github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android
另外,我被要求尝试使用本地代码路径来加载文件:graphsgithub.com/tensorflow/tensorflow/issues/9343#issuecomment-295959477 但是我并不精通C++ / NDK(我只是不使用它) 因此有一个可用于加载文件的.cc文件。 如果有人能够帮助我将其集成到TensorFlow示例Android项目中,我将不胜感激。 但是我仍然希望有一种无须NDK的解决方案,因为对于2016-2017年的Android设备而言,200MB真的是这样一个巨大的文件吗?