Skip to content

Commit

Permalink
bridge: replace the WeakReference to the engine in `SVGAnimationEng…
Browse files Browse the repository at this point in the history
…ine` with a `SoftReference`

In `SVGAnimationEngine` the animation engine is kept as a `WeakReference`, but
with the current JVMs those references do not last very long. This may cause
some animations to not happen.

Also due to that, the `SwingMemoryLeakTest` requires at least 4GB of heap space
to pass and is triggering failures. Switching the `WeakReference` by a `SoftReference`
may seem a bit radical but is the most conservative way to fix this.

As a consequence of this change, the stress-tests for `WeakReference`d objects
(that were failing, as explained above) are no longer executed during the build.
It could be said that such tests achieved what they were intended for: check for
the validity of the `WeakReference` approach.

Closes #103.
  • Loading branch information
carlosame committed Jun 26, 2024
1 parent 32c8e03 commit b571a2b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.awt.Color;
import java.awt.Paint;
import java.lang.ref.WeakReference;
import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -804,11 +804,11 @@ protected static class AnimationTickRunnable implements RunnableQueue.IdleRunnab
protected int timeIndex;

/**
* A weak reference to the SVGAnimationEngine this AnimationTickRunnable is for.
* We make this a WeakReference so that a ticking animation engine does not
* A soft reference to the SVGAnimationEngine this AnimationTickRunnable is for.
* We make this a SoftReference so that a ticking animation engine does not
* prevent from being GCed.
*/
protected WeakReference<SVGAnimationEngine> engRef;
protected SoftReference<SVGAnimationEngine> engRef;

/**
* The maximum number of consecutive exceptions to allow before stopping the
Expand All @@ -828,7 +828,7 @@ protected static class AnimationTickRunnable implements RunnableQueue.IdleRunnab
*/
public AnimationTickRunnable(RunnableQueue q, SVGAnimationEngine eng) {
this.q = q;
this.engRef = new WeakReference<>(eng);
this.engRef = new SoftReference<>(eng);
// Initialize the past times to 100ms.
Arrays.fill(times, 100);
sumTime = 100 * NUM_TIMES;
Expand Down
2 changes: 0 additions & 2 deletions echosvg-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ tasks.register('memleakTest', Test) {
classpath = testing.suites.test.sources.runtimeClasspath
}

check.dependsOn memleakTest

/*
* JMH benchmarks
*/
Expand Down

0 comments on commit b571a2b

Please sign in to comment.