如果我使用solo.takeScreenshot,由于'java.lang.OutOfMemoryError'导致仪表运行失败

时间:2014-01-09 10:24:10

标签: android robotium

当我在使用Robotium进行测试时启用截屏时,我遇到了OutOfMemoryException。

[2014-01-09 10:53:28 - MyAppTest] Test run failed: Instrumentation run failed due to 'java.lang.OutOfMemoryError'

在此消息的末尾,您可以找到logcat错误。

正如您在下面的代码中看到的那样,我有一个变量可以让我决定是否要截取屏幕截图;如果我启用截屏,我运行大约10个测试方法之后会出现上述错误(每次测试都需要1到10个截图)。

这是我运行的代码:

public class TestApk extends ActivityInstrumentationTestCase2{
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.xxx.xxxnew.activities.XxxActivity";
    private static Class launcherActivityClass;

    private static final boolean ENABLE_SCREENSHOT_CAPTURE = true;
    private static final int SLEEP_TIME = 4000;

    static{
        try{
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e){
            throw new RuntimeException(e); 
        } 
    }

    @SuppressLint("NewApi")
    public TestApk()throws ClassNotFoundException{
        super(launcherActivityClass); 
    }

    private Solo solo;

    @Override
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(),getActivity());
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }

    public void test_login_001(){
    // ... some Robotium code
    if(ENABLE_SCREENSHOT_CAPTURE){
            solo.sleep(SLEEP_TIME);
            solo.takeScreenshot("login_001_c");
        }
    // ... some Robotium code   
    if(ENABLE_SCREENSHOT_CAPTURE){
            solo.sleep(SLEEP_TIME);
            solo.takeScreenshot("login_001_c");
        }
    }   

    public void test_login_002_a(){
    // ... some Robotium code
    if(ENABLE_SCREENSHOT_CAPTURE){
            solo.sleep(SLEEP_TIME);
            solo.takeScreenshot("login_002_a");
        }
    // ... some Robotium code   
    if(ENABLE_SCREENSHOT_CAPTURE){
            solo.sleep(SLEEP_TIME);
            solo.takeScreenshot("login_001_2");
        }
    }   

    //other test methods
}

这是来自LogCat的错误:

01-09 11:16:55.619: I/dalvikvm-heap(13717): Forcing collection of SoftReferences for 8294416-byte allocation
01-09 11:16:55.659: D/dalvikvm(13717): GC_BEFORE_OOM freed 10K, 14% free 112688K/129624K, paused 37ms, total 38ms
01-09 11:16:55.659: E/dalvikvm-heap(13717): Out of memory on a 8294416-byte allocation.
01-09 11:16:55.659: I/dalvikvm(13717): "main" prio=5 tid=1 RUNNABLE
01-09 11:16:55.659: I/dalvikvm(13717):   | group="main" sCount=0 dsCount=0 obj=0x40e68b38 self=0x40e4a0b8
01-09 11:16:55.659: I/dalvikvm(13717):   | sysTid=13717 nice=0 sched=0/0 cgrp=apps handle=1074288092
01-09 11:16:55.659: I/dalvikvm(13717):   | state=R schedstat=( 0 0 0 ) utm=1943 stm=453 core=0
01-09 11:16:55.659: I/dalvikvm(13717):   at android.graphics.Bitmap.nativeCopy(Native Method)
01-09 11:16:55.659: I/dalvikvm(13717):   at android.graphics.Bitmap.copy(Bitmap.java:471)
01-09 11:16:55.659: I/dalvikvm(13717):   at com.jayway.android.robotium.solo.ScreenshotTaker.getBitmapOfView(ScreenshotTaker.java:215)
01-09 11:16:55.659: I/dalvikvm(13717):   at com.jayway.android.robotium.solo.ScreenshotTaker.access$400(ScreenshotTaker.java:32)
01-09 11:16:55.659: I/dalvikvm(13717):   at com.jayway.android.robotium.solo.ScreenshotTaker$ScreenshotRunnable.run(ScreenshotTaker.java:326)
01-09 11:16:55.659: I/dalvikvm(13717):   at android.os.Handler.handleCallback(Handler.java:725)
01-09 11:16:55.659: I/dalvikvm(13717):   at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 11:16:55.659: I/dalvikvm(13717):   at android.os.Looper.loop(Looper.java:137)
01-09 11:16:55.659: I/dalvikvm(13717):   at android.app.ActivityThread.main(ActivityThread.java:5328)
01-09 11:16:55.659: I/dalvikvm(13717):   at java.lang.reflect.Method.invokeNative(Native Method)
01-09 11:16:55.659: I/dalvikvm(13717):   at java.lang.reflect.Method.invoke(Method.java:511)
01-09 11:16:55.659: I/dalvikvm(13717):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-09 11:16:55.659: I/dalvikvm(13717):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-09 11:16:55.659: I/dalvikvm(13717):   at dalvik.system.NativeStart.main(Native Method)
01-09 11:16:55.659: D/AbsListView(13717): [unregisterDoubleTapMotionListener]
01-09 11:16:55.659: I/MotionRecognitionManager(13717):   .unregisterListener : / listener count = 0->0,  
01-09 11:16:55.659: D/AbsListView(13717): unregisterIRListener() is called 
01-09 11:16:55.659: D/AndroidRuntime(13717): Shutting down VM
01-09 11:16:55.659: W/dalvikvm(13717): threadid=1: thread exiting with uncaught exception (group=0x40e67ac8)
01-09 11:16:55.669: E/AndroidRuntime(13717): FATAL EXCEPTION: main
01-09 11:16:55.669: E/AndroidRuntime(13717): java.lang.OutOfMemoryError
01-09 11:16:55.669: E/AndroidRuntime(13717):    at android.graphics.Bitmap.nativeCopy(Native Method)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at android.graphics.Bitmap.copy(Bitmap.java:471)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at com.jayway.android.robotium.solo.ScreenshotTaker.getBitmapOfView(ScreenshotTaker.java:215)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at com.jayway.android.robotium.solo.ScreenshotTaker.access$400(ScreenshotTaker.java:32)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at com.jayway.android.robotium.solo.ScreenshotTaker$ScreenshotRunnable.run(ScreenshotTaker.java:326)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at android.os.Handler.handleCallback(Handler.java:725)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at android.os.Looper.loop(Looper.java:137)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at android.app.ActivityThread.main(ActivityThread.java:5328)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at java.lang.reflect.Method.invoke(Method.java:511)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-09 11:16:55.669: E/AndroidRuntime(13717):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您可以通过为屏幕截图保存较低质量的图像来减少使用的内存。

int quality = 50; // On a scale of 1-100
solo.takeScreenshot("login_001_c", quality);

但是机器人中可能存在导致问题的内存泄漏。如果是这样,您应该在https://code.google.com/p/robotium/issues/list

提交问题