0

我正在创建一个面部检测器应用程序,其中使用位图存储图像。代码中没有此类错误,但在设备上运行时显示异常。以下是 Logcat 详细信息。任何形式的帮助将不胜感激。提前致谢

 11-06 15:50:49.340: D/dalvikvm(32555): GC_FOR_ALLOC freed 43K, 8% free 12131K/13059K, paused 21ms, total 25ms
    11-06 15:50:49.390: I/dalvikvm-heap(32555): Grow heap (frag case) to 22.111MB for 10036240-byte allocation
    11-06 15:50:49.425: D/dalvikvm(32555): GC_CONCURRENT freed <1K, 5% free 21931K/22919K, paused 14ms+2ms, total 35ms
    11-06 15:50:49.730: D/dalvikvm(32555): GC_FOR_ALLOC freed 0K, 5% free 21932K/22919K, paused 10ms, total 10ms
    11-06 15:50:49.920: I/dalvikvm-heap(32555): Grow heap (frag case) to 60.396MB for 40144912-byte allocation
    11-06 15:50:49.940: D/dalvikvm(32555): GC_CONCURRENT freed 0K, 2% free 61136K/62151K, paused 1ms+2ms, total 20ms
    11-06 15:50:50.310: D/dalvikvm(32555): GC_FOR_ALLOC freed 9802K, 18% free 51335K/62151K, paused 15ms, total 15ms
    11-06 15:50:50.310: I/dalvikvm-heap(32555): Forcing collection of SoftReferences for 20072464-byte allocation
    11-06 15:50:50.325: D/dalvikvm(32555): GC_BEFORE_OOM freed 9K, 18% free 51326K/62151K, paused 17ms, total 17ms
    11-06 15:50:50.325: E/dalvikvm-heap(32555): Out of memory on a 20072464-byte allocation.
    11-06 15:50:50.325: I/dalvikvm(32555): "main" prio=5 tid=1 RUNNABLE
    11-06 15:50:50.325: I/dalvikvm(32555):   | group="main" sCount=0 dsCount=0 obj=0x41982508 self=0x419728e8
    11-06 15:50:50.325: I/dalvikvm(32555):   | sysTid=32555 nice=0 sched=0/0 cgrp=apps handle=1074306864
    11-06 15:50:50.330: I/dalvikvm(32555):   | schedstat=( 683836904 179752181 2016 ) utm=39 stm=29 core=3
    11-06 15:50:50.335: I/dalvikvm(32555):   at android.media.FaceDetector.<init>(FaceDetector.java:~121)
    11-06 15:50:50.335: I/dalvikvm(32555):   at com.example.facedetection.FaceDetection$MyView.<init>(FaceDetection.java:56)
    11-06 15:50:50.335: I/dalvikvm(32555):   at com.example.facedetection.FaceDetection.onCreate(FaceDetection.java:23)
    11-06 15:50:50.355: I/dalvikvm(32555):   at android.app.Activity.performCreate(Activity.java:5206)
    11-06 15:50:50.355: I/dalvikvm(32555):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
    11-06 15:50:50.360: I/dalvikvm(32555):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
    11-06 15:50:50.360: I/dalvikvm(32555):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
    11-06 15:50:50.360: I/dalvikvm(32555):   at android.app.ActivityThread.access$600(ActivityThread.java:140)
    11-06 15:50:50.360: I/dalvikvm(32555):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
    11-06 15:50:50.360: I/dalvikvm(32555):   at android.os.Handler.dispatchMessage(Handler.java:99)
    11-06 15:50:50.365: I/dalvikvm(32555):   at android.os.Looper.loop(Looper.java:137)
    11-06 15:50:50.365: I/dalvikvm(32555):   at android.app.ActivityThread.main(ActivityThread.java:4898)
    11-06 15:50:50.365: I/dalvikvm(32555):   at java.lang.reflect.Method.invokeNative(Native Method)
    11-06 15:50:50.370: I/dalvikvm(32555):   at java.lang.reflect.Method.invoke(Method.java:511)
    11-06 15:50:50.370: I/dalvikvm(32555):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    11-06 15:50:50.370: I/dalvikvm(32555):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    11-06 15:50:50.370: I/dalvikvm(32555):   at dalvik.system.NativeStart.main(Native Method)
    11-06 15:50:50.370: D/AndroidRuntime(32555): Shutting down VM
    11-06 15:50:50.370: W/dalvikvm(32555): threadid=1: thread exiting with uncaught exception (group=0x419812a0)
    11-06 15:50:50.390: E/AndroidRuntime(32555): FATAL EXCEPTION: main
    11-06 15:50:50.390: E/AndroidRuntime(32555): java.lang.OutOfMemoryError
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.media.FaceDetector.<init>(FaceDetector.java:121)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at com.example.facedetection.FaceDetection$MyView.<init>(FaceDetection.java:56)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at com.example.facedetection.FaceDetection.onCreate(FaceDetection.java:23)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.app.Activity.performCreate(Activity.java:5206)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.os.Handler.dispatchMessage(Handler.java:99)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.os.Looper.loop(Looper.java:137)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at android.app.ActivityThread.main(ActivityThread.java:4898)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at java.lang.reflect.Method.invokeNative(Native Method)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at java.lang.reflect.Method.invoke(Method.java:511)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
    11-06 15:50:50.390: E/AndroidRuntime(32555):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

0

当您 Bitmap 成功完成其工作时,那时,位图回收并将其分配给 null。

Bitmap bitmap;

使用 Bitmap 完成工作后。

bitmap.recycle();
bitmap=null;
于 2013-11-06T10:52:37.557 回答
0

您需要回收不再使用的位图。您也可以尝试阅读以有效加载位图:http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html

于 2013-11-06T20:37:31.373 回答
0

请在使用后回收位图,如下所示:

@Override
    public void onDestroy()
    {   
        cleanUp();
        super.onDestroy();
    }

    private void cleanUp()
    {   
        if(bmp!=null){
            bmp.recycle();
            bmp=null;
        }
        if(realBmp!=null){
            realBmp.recycle();
            realBmp=null;
        }      
        System.gc();
        Runtime.getRuntime().gc();  
    }
于 2013-11-06T10:38:26.493 回答