在Android中运行浓缩咖啡测试时出现内存不足的问题

Dar*_*han 5 java android out-of-memory android-espresso

在Kitkat和Lollipop设备上运行咖啡测试套件时,我一直面临这个问题(OutOfMemory)。该套件可在棉花糖设备上完美运行。以下是更多详细信息,如果能在确定此问题的根本原因方面获得任何帮助,我将不胜感激。以下是更多详细信息:

  1. 仅当我一次运行整个套件(175个测试)时才会发生OutofMemory,并且如果我分别运行每个测试集也不会发生此问题。
  2. 我尝试使用System.gc(),该脚本在运行每个方案后都会被调用,但仍然观察到OutOfmemory异常。
  3. 每次我运行时,异常都会在不同的测试脚本上发生,并且在特定功能/测试脚本上不一致。
  4. 应用程序中使用的位图已被压缩,因此不确定是否与图像大小有关。
  5. 以下是最近运行的日志:

    java.lang.Thread.run(Thread.java:818)01-26 14:42:39.770 3476-4264 /?E /看门狗:!@Sync 579 [01-26 14:42:39.778] 01-26 14:42:42.470 3476-3476 /?E / MotionRecognitionService:支持TA〜01-26 14:42:45.180 4265-4317 /?E / ContactsProvider_EventLog:刷新缓冲区到文件cnt:3大小:0Kb持续时间:39ms lastUpdatedAfter:180367ms 01-26 14:42:52.510 3476-3476 /?E / MotionRecognitionService:支持TA〜01-26 14:42:55.320 446-454 /?E / System:终结器抛出未捕获的异常01-26 14:42:56.900 446-454 /?E / System:java.lang.OutOfMemoryError:尝试抛出OutOfMemoryError时抛出了OutOfMemoryError;没有可用的堆栈跟踪01-26 14:42:56.910 446-1088 /?E / CrashReporting:ParseCrashReporting捕获了com.capitalone.mobilebanking.dev.debug的OutOfMemoryError异常。建筑报告。01-26 14:42:56。910 446-18726 /?E / CrashReporting:ParseCrashReporting捕获了com.capitalone.mobilebanking.dev.debug的OutOfMemoryError异常。建筑报告。01-26 14:42:56.910 446-446 /?E / MonitoringInstrumentation:线程[main,5,main]遇到异常。将线程状态转储到输出并固定到峡湾。 java.lang.OutOfMemoryError:无法分配40字节的分配空间,其中包含3960个空闲字节和3KB,直到OOM 158),位于android.app.ActivityThread.main(ActivityThread.java:7224),位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:的java.lang.reflect.Method.invoke(本机方法)处): 1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)01-26 14:42:56.910 446-18726 /?E / AndroidRuntime:ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)01-26 14:42:56.910 446-18726 /?E / AndroidRuntime:ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)01-26 14:42:56.910 446-18726 /?E / AndroidRuntime:致命异常:IntentService [AlertSubscriptionService] 进程:com.capitalone.mobilebanking.dev.debug,PID:446 java.lang.OutOfMemoryError:尝试抛出OutOfMemoryError时抛出了OutOfMemoryError;没有可用的堆栈跟踪01-26 14:42:56.910 446-1088 /?E / CrashReporting:处理崩溃的异常 java.lang.OutOfMemoryError:无法分配4092字节的分配,其中包含3928个空闲字节和3KB,直到OOM 在java.lang.String.StringBuffer.append(StringBuffer.java:219)在java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:146)在java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)。 regex.Matcher.appendTail(Matcher.java:285),位于java.util.regex.Matcher.replaceAll(Matcher.java:321),位于gherkin.formatter.PrettyFormatter.indent(PrettyFormatter.java:469),位于gherkin.formatter.PrettyFormatter .printError(PrettyFormatter.java:385),位于Java的gherkin.formatter.PrettyFormatter.eof(PrettyFormatter.java:421)的gherkin.formatter.PrettyFormatter.replay(PrettyFormatter.java:121)的gherkin.formatter.PrettyFormatter.printSteps(PrettyFormatter.java:133)处。 lang.reflect.Method.invoke(本机方法)at黄瓜.runtime.Utils $ 1.call(Utils.java:34)at黄瓜.runtime.Timeout.timeout(Timeout.java:13)at黄瓜.runtime.Utils.invoke (Utils.java:30)在cucumber.runtime.RuntimeOptions $ 1.invoke(RuntimeOptions。黄瓜上$ java.lang.reflect.Proxy.invoke(Proxy.java:393)上的java.lang.ref($ Proxy11.eof(未知源)上的黄瓜.runtime.model.CucumberFeature.run(CucumberFeature.java:165)上的java:243)。黄瓜.api.android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:88)上的runtime.android.CucumberExecutor.execute(CucumberExecutor.java:113)位于com.capitalone.mobilebanking.espressoTests.Instrumentation.onStart(Instrumentation.java:133)在android.app.Instrumentation243)处$ java.lang.reflect.Proxy.invoke(Proxy.java:393)处$ cumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)处$ Proxy11.eof(未知源)。 android.CucumberExecutor.execute(CucumberExecutor.java:113)位于cu.api.android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:88)位于com.capitalone.mobilebanking.espressoTests.Instrumentation.onStart(Instrumentation.java:133) .app。仪器243)处$ java.lang.reflect.Proxy.invoke(Proxy.java:393)处$ cumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)处$ Proxy11.eof(未知源)。 android.CucumberExecutor.execute(CucumberExecutor.java:113)位于cu.api.android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:88)位于com.capitalone.mobilebanking.espressoTests.Instrumentation.onStart(Instrumentation.java:133) .app。仪器393)在$ Proxy11.eof(未知来源)在cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)在cucumber.runtime.android.CucumberExecutor.execute(CucumberExecutor.java:113)。 android.app.Instrumentation上的com.capitalone.mobilebanking.espressoTests.Instrumentation.onStart(Instrumentation.java:133)上的android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:88)393)在$ Proxy11.eof(未知来源)在cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)在cucumber.runtime.android.CucumberExecutor.execute(CucumberExecutor.java:113)。 android.app.Instrumentation上的com.capitalone.mobilebanking.espressoTests.Instrumentation.onStart(Instrumentation.java:133)上的android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:88)在com.capitalone.mobilebanking.espressoTests上在cucumber.api.android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:88)处cucumber.runtime.android.CucumberExecutor.execute(CucumberExecutor.java:113)处运行(CucumberFeature.java:165) .Instrumentation.onStart(Instrumentation.java:133)在android.app.Instrumentation在com.capitalone.mobilebanking.espressoTests上在cucumber.api.android.CucumberInstrumentationCore.start(CucumberInstrumentationCore.start(CucumberInstrumentationCore.java:88)处cucumber.runtime.android.CucumberExecutor.execute(CucumberExecutor.java:113)处运行(CucumberFeature.java:165) .Instrumentation.onStart(Instrumentation.java:133)在android.app.Instrumentation88)在com.capitalone.mobilebanking.espressoTests.Instrumentation.onStart(Instrumentation.java:133)在android.app.Instrumentation88)在com.capitalone.mobilebanking.espressoTests.Instrumentation.onStart(Instrumentation.java:133)在android.app.Instrumentation

Zak*_*ria 5

当我开始同时运行 300 多个 Espresso 测试时,我遇到了同样的问题。显然,每次测试后内存都没有被清除,这导致了 OOM 错误(OutOfMemoryError)。

为了解决这个问题,我开始使用Android Test Orchestrator,它为您的测试环境提供以下好处:

  • 最小共享状态。每个测试都在其自己的 Instrumentation 实例中运行。因此,如果您的测试共享应用程序状态,则每次测试后大部分共享状态都会从设备的 CPU 或内存中删除。
  • 要在每次测试后从设备的 CPU 和内存中删除所有共享状态,请使用clearPackageData 标志。
  • 崩溃是孤立的。即使一个测试崩溃,它也只会删除它自己的 Instrumentation 实例,因此套件中的其他测试仍然运行。

我在项目构建 gradle 文件中添加了Android Test Orchestrator来解决该问题:

android {
  defaultConfig {
   ...
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

   // The following argument makes the Android Test Orchestrator run its
   // "pm clear" command after each test invocation. This command ensures
   // that the app's state is completely cleared between tests.
   testInstrumentationRunnerArguments clearPackageData: 'true'
 }

  testOptions {
    execution 'ANDROIDX_TEST_ORCHESTRATOR'
  }
}

dependencies {
  androidTestImplementation 'androidx.test:runner:1.1.0'
  androidTestUtil 'androidx.test:orchestrator:1.1.0'
}
Run Code Online (Sandbox Code Playgroud)

  • 应该注意的是,添加“testInstrumentationRunnerArguments clearPackageData: 'true'”还将清除测试之间的任何代码覆盖率数据。因此,如果您想要浓缩咖啡测试的覆盖率报告,除了可以隔离故障并让测试套件的其余部分运行之外,orchestrator 毫无用处。 (5认同)