takahirom / roborazzi

Make JVM Android integration test visible 🤖📸
https://takahirom.github.io/roborazzi/
Apache License 2.0
649 stars 24 forks source link

OutOfMemoryError when capturing moderate amount of screens or states #393

Closed mlilienberg closed 1 month ago

mlilienberg commented 1 month ago

Many thanks for creating and maintaining this library. I came across a few issues and one is that the test fails with out of memory exception when i include too many view interactions in the captureRoboGif block or when i use the rule. The

This is the stacktrace:

Java heap space
java.lang.OutOfMemoryError: Java heap space
    at java.desktop/java.awt.image.DataBufferInt.<init>(Unknown Source)
    at java.desktop/java.awt.image.Raster.createPackedRaster(Unknown Source)
    at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
    at java.desktop/java.awt.image.BufferedImage.<init>(Unknown Source)
    at com.github.takahirom.roborazzi.BitmapToBufferedImageConverter.bitmapToImage(BitmapToBufferedImageConverter.kt:20)
    at com.github.takahirom.roborazzi.BitmapToBufferedImageConverter.convert(BitmapToBufferedImageConverter.kt:14)
    at com.github.takahirom.roborazzi.AwtRoboCanvas$drawImage$1.invoke(AwtRoboCanvas.kt:50)
    at com.github.takahirom.roborazzi.AwtRoboCanvas$drawImage$1.invoke(AwtRoboCanvas.kt:48)
    at com.github.takahirom.roborazzi.AwtRoboCanvasKt.graphics(AwtRoboCanvas.kt:594)
    at com.github.takahirom.roborazzi.AwtRoboCanvasKt.access$graphics(AwtRoboCanvas.kt:1)
    at com.github.takahirom.roborazzi.AwtRoboCanvas.drawImage(AwtRoboCanvas.kt:48)
    at com.github.takahirom.roborazzi.RoborazziKt.capture(Roborazzi.kt:621)
    at com.github.takahirom.roborazzi.ImageCaptureViewAction.perform(Roborazzi.kt:587)
    at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:2)
    at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:25)
    at androidx.test.espresso.ViewInteraction.-$$Nest$mdoPerform(ViewInteraction.java)
    at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:7)
    at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:1)
    at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:942)
    at java.base/java.lang.invoke.LambdaForm$DMH/0x0000000800295c00.invokeStatic(LambdaForm$DMH)
    at java.base/java.lang.invoke.LambdaForm$MH/0x00000008002a2c00.guardWithCatch(LambdaForm$MH)
    at java.base/java.lang.invoke.DelegatingMethodHandle$Holder.delegate(Unknown Source)
    at java.base/java.lang.invoke.LambdaForm$MH/0x00000008002a3c00.guard(LambdaForm$MH)
    at java.base/java.lang.invoke.LambdaForm$MH/0x00000008002a4000.linkToCallSite(LambdaForm$MH)
    at android.os.Handler.handleCallback(Handler.java)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at java.base/java.lang.invoke.LambdaForm$DMH/0x0000000800295400.invokeSpecial(LambdaForm$DMH)
    at java.base/java.lang.invoke.LambdaForm$MH/0x00000008002aac00.guardWithCatch(LambdaForm$MH)
    at java.base/java.lang.invoke.DelegatingMethodHandle$Holder.delegate(Unknown Source)
    at java.base/java.lang.invoke.LambdaForm$MH/0x00000008002ac400.guard(LambdaForm$MH)
    at java.base/java.lang.invoke.LambdaForm$MH/0x00000008002ac800.linkToCallSite(LambdaForm$MH)

I had a look at com.github.takahirom.roborazzi.RoborazziKt.capture(Roborazzi.kt:621) where the app is crashing and i am wondering if it could help to recycle the bitmaps after usage. I tried to call it from the debugger , which had no effect. I also tried to call System.gc at different places in my test but this did not help either. Only reducing the amount of screens wrapped by the call helped as interim workaround.

takahirom commented 1 month ago

Thanks! I've noticed a similar OutOfMemoryError issue was discussed in Issue #272. It might be helpful to consider some of the approaches mentioned there.

Additionally, it might be worth trying to run your tests with VisualVM to monitor memory usage and identify potential leaks.

mlilienberg commented 1 month ago

Thanks a lot. I was not aware that unit tests require extra declaration for heap space definition. I was wondering why the changes in gradle.properties had no effect. Never stop learning. Setting the heap space might be something worth to mention in the documentation.