From 1f8cc0a2206923089fe3c568a4fe81bda5622942 Mon Sep 17 00:00:00 2001 From: Carlos Amengual Date: Wed, 26 Jun 2024 17:39:03 +0200 Subject: [PATCH] bridge: replace the `WeakReference` to the engine in `SVGAnimationEngine` 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. --- .../io/sf/carte/echosvg/bridge/SVGAnimationEngine.java | 10 +++++----- echosvg-test/build.gradle | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/echosvg-bridge/src/main/java/io/sf/carte/echosvg/bridge/SVGAnimationEngine.java b/echosvg-bridge/src/main/java/io/sf/carte/echosvg/bridge/SVGAnimationEngine.java index ca7808050..1a21ae7a6 100644 --- a/echosvg-bridge/src/main/java/io/sf/carte/echosvg/bridge/SVGAnimationEngine.java +++ b/echosvg-bridge/src/main/java/io/sf/carte/echosvg/bridge/SVGAnimationEngine.java @@ -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; @@ -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 engRef; + protected SoftReference engRef; /** * The maximum number of consecutive exceptions to allow before stopping the @@ -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; diff --git a/echosvg-test/build.gradle b/echosvg-test/build.gradle index af63c73fd..5ffc54e4b 100644 --- a/echosvg-test/build.gradle +++ b/echosvg-test/build.gradle @@ -126,8 +126,6 @@ tasks.register('memleakTest', Test) { classpath = testing.suites.test.sources.runtimeClasspath } -check.dependsOn memleakTest - /* * JMH benchmarks */