-
Notifications
You must be signed in to change notification settings - Fork 104
GCVerifier
The GCVerifier
is a small testing util that we are using internally when it comes to Garbage Collection.
It is available in the mvvmfx-testing-utils module.
The main problem with Garbage Collection and JavaFX is that JavaFX internally uses WeakReference and WeakListeners in some places and therefore it is possible that code that depends on data-binding or listenes aren't working as expected. In most cases these problems can't be found with simple JUnit tests because no GC is done during test execution.
In these cases the GCVerifier
can help to reproduce problems with GC in unit tests.
With the static method GCVerifier.forceGC()
it is possible to "force" the JavaVM to run garbage collection. This method can be called in your test after the expected data-bindings/listeners are setup. If they are effected by GC, they will now break your unit test.
Please note: This method should only be used in tests. It's typically a bad idea to force Garbage Collection in production code as it can drastically reduce the performance of the application due to "stop the world" problems (the application stops for a moment to run garbage collection).
In some situations we need to be sure that an object is available for garbage collection (i.e. no other reference is available to the object). An example is the handling of listeners. After a listener is removed from an observable it should be available for GC. Otherwise the listener may itself could prevent other objects from being garbage collected. To verifiy this we are using the GCVerifier
:
Object reference ...
GCVerifier verifier = GCVerifier.create(reference);
... // some code after that the reference should be free
verifier.verifiy();
If the given reference isn't available for garbage collection, the verifiy()
method will throw an AssertionError
so that a JUnit test will fail.