gpt4 book ai didi

android - 如果我使用 solo.takeScreenshot,检测运行会因 'java.lang.OutOfMemoryError' 而失败

转载 作者:行者123 更新时间:2023-11-30 03:10:45 24 4
gpt4 key购买 nike

在使用 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)

最佳答案

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

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

但是 robotium 中可能存在内存泄漏导致了问题。如果是这样,您应该在此处提出问题 https://code.google.com/p/robotium/issues/list

关于android - 如果我使用 solo.takeScreenshot,检测运行会因 'java.lang.OutOfMemoryError' 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21017511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com