Out of memory: Java heap space and very slow test execution


We have a project with 300 test methods and on weaker machines, our build fails because of "Out of memory: Java heap space"

What we have noticed that each test method is creating a new context object.
Besides out of memory exception test execution is very slow (For each test class we are registering a minimum of necessary services and appropriate resource resolver type).



Stefan Seifert
September 26, 2019, 8:05 PM

What we have noticed that each test method is creating a new context object

this is by design to make sure each test start with a clean and empty context. discussion about adding @BeforeAll support takes place in

besides this it's a bit difficult to find a solution without more information.

regarding the heap space: AEM Mocks itself should not "leak" memory - if you can see something like this e.g. in a java profiler please report it back. the reason may also lay in your unit tests and not necessarily in AEM Mocks itself. e.g. if you are creating AEM assets on the fly with very high resolutions this can take up a lot of memory. you might also consider increasing the heap space for maven by setting e.g.

concerning the execution performance: sling mocks and aem mocks are designed for very fast test execution, but of course it still take a bit time to set up and tear down a context with full sling support.
288 tests in 42sec is not so bad in my point of view - this is 0,14 sec per test case. what is you expectation?

Matija Kovacek
October 5, 2019, 1:32 PM

Hi Stefan,

Here is a heap dump file, when I will have time I will try to analyze it.

On the first look, I would say that leaking is related to JSON load into context, or it is just not enough heap space because we load JSON file for each test method - since that's how it works per design (context creation).

AemContext -> ContentLoader -> JsonContentParser -> JsonReaderFactoryImpl -> JsonParserFactoryImpl -> BufferStrategy$CharBufferQueueProvider

Regarding speed, everything is mentioned in WTES-42, we have a workaround (multiple test cases invoked from one test method).

Stefan Seifert
October 21, 2019, 3:13 PM

thanks, i'll have a look at the json loading part.




Matija Kovacek




Affects versions