Answer the question
In order to leave comments, you need to log in
How to delete objects in Java so that TestNG does not catch OutOfMemory?
Yes, I know that in Java, objects cannot be deleted manually - the garbage collector works as it wants. The question is not about that.
There are tests with TestNG.
One of these tests takes two screenshots and compares how similar they are.
This test is repeated about 50 times for different websites, but already somewhere around 20m it crashes with OutOfMemory.
When debugging, it turned out that org.testng.Reporter logs all the properties of the test object, which has these two screenshots, which take up a lot of space, with each assert (or each test).
The screenshots themselves are written to disk, and are not needed in org.testng.Reporter, but how to make sure that they do not get there?
I tried like this, but they still end up in org.testng.Reporter, which hangs in memory and is not flushed to disk until the last:
frameImg = takeEmbeddedScreenshot()
actualImg = takeActualScreenshot(url)
boolean compareRes = compareScreenShots(frameImg, actualImg, "build/screenshots/proxy")
frameImg = null
actualImg = null
System.gc ()
assert compareRes, "Too many differences between the two panes, this URL fails: ${url}"
Answer the question
In order to leave comments, you need to log in
An example of a memory leak in Java. I have no idea how TestNG works and whether it can be hacked, but I think the easiest solution would be to change the screenshot functions so that they reuse objects, or, if this is also difficult, copy the screenshots into two buffers before comparing, which will be the fields of the test class , (ByteBuffer, or whatever it would be easier to copy the screenshot bytes into), and compare them already. Then TestNG will log references to two objects, the same all the time, and new screenshots will be collected by the garbage collector.
No, actually there is no leak here. This is such a (crooked) design: the idea, as I understand it, is to write down as much data as possible so that you can debug post-fact.
Regarding the two buffers: there are only two of them, and everything is overwritten in them every time. The fact is that TestNG runs the same test many times, only with different data. And even if the links remain the same, it will still copy them again.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question