0

I am writing a lot of JUnit tests for my Javafx application. I have reached 170 tests by now, but from now on I always get a java.lang.OutOfMemoryError: Java heap space when I run all my tests. Then I took a look at the resource consumption when I run my tests and with every new test the memory usage increases by 40MB, but I think after a test the used memory should go down again or?. Is it normal that when testing JavaFx applications (almost all tests are GUI tests), the memory usage increases or do you have to call the garbage collector manually or something like that? I relatively new to JUnit testing, so correct me if I say something wrong.

Im using these two methods to make a setup before each test and after the test a cleanup.

@BeforeEach
public void setup() throws Exception {
    stage = FxToolkit.registerPrimaryStage();
    app = (MyApplication) FxToolkit.setupApplication(MyApplication.class);
    FxToolkit.showStage();
    WaitForAsyncUtils.waitForFxEvents();
}

@AfterEach
public void afterEachTest() throws Exception {

    FxToolkit.hideStage();
    release(new KeyCode[]{});
    release(new MouseButton[]{});
    FxToolkit.cleanupStages();
    FxToolkit.cleanupApplication(app);
}

EDIT: Do I have to set the app to null in the afterEachTest() after the FxToolkit.cleanupApplication(app)?

EDIT2: I am sure, the test on its own can't get out of memory because they are lightweight. Could be the problem, that JUnit doesn't release any memory until all test ran? I am using currently JUnit 5.

Gregor
  • 409
  • 1
  • 5
  • 12
  • [mcve] please.. – kleopatra Jun 04 '20 at 22:27
  • Usually you don't have to call the garbage collector by yourself, you should do a memory dump e see what is the most heavy object which is consuming your heap space – Snix Jun 05 '20 at 21:05
  • 1
    I had a problem with heap space because into my code there was a resource i wasn't closing properly and this led to an hell, it may be your case, just do a memory dump and see the memory allocation – Snix Jun 05 '20 at 21:05
  • Where does the memory run out? In your IDE? HAve you tried to set the JVM flags for heap memory limits? Is this related https://stackoverflow.com/questions/7579776/how-to-set-jvm-parameters-for-junit-unit-tests – Jocke Jun 09 '20 at 10:32
  • @Jocke: I run these test on a Jenkins and there I run out of memory. I tried to increase the heap size there but nothing worked.. I set the heap size in the jenkins file as maven_ops and in the surefire plugin but nothing worked on the jenkins.. – Gregor Jun 09 '20 at 18:19
  • Why don't you use a [memory profiler](https://visualvm.github.io/) @Gregor to check it? – Snix Jun 09 '20 at 23:38

0 Answers0