From cb2ae0295fae901ce58abf49f1b93be9494c3473 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Wed, 9 Sep 2020 16:47:45 +0400 Subject: [PATCH] * Performance: improved memory usage for images (now xmage will not eat all available memory on 2GB+ settings, see #6375); --- .../client/util/SoftValuesLoadingCache.java | 17 +++++++++++------ .../mage/card/arcane/CardPanelRenderImpl.java | 8 +++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/util/SoftValuesLoadingCache.java b/Mage.Client/src/main/java/mage/client/util/SoftValuesLoadingCache.java index 40e1550c91..0341405dc8 100644 --- a/Mage.Client/src/main/java/mage/client/util/SoftValuesLoadingCache.java +++ b/Mage.Client/src/main/java/mage/client/util/SoftValuesLoadingCache.java @@ -1,23 +1,28 @@ package mage.client.util; -import static com.google.common.cache.CacheBuilder.newBuilder; - -import java.util.Optional; -import java.util.concurrent.ExecutionException; - import com.google.common.base.Function; import com.google.common.cache.CacheLoader; import com.google.common.cache.ForwardingLoadingCache; import com.google.common.cache.LoadingCache; import org.apache.log4j.Logger; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static com.google.common.cache.CacheBuilder.newBuilder; + public class SoftValuesLoadingCache extends ForwardingLoadingCache> { private final LoadingCache> cache; private static final Logger logger = Logger.getLogger(SoftValuesLoadingCache.class); public SoftValuesLoadingCache(CacheLoader> loader) { - cache = newBuilder().softValues().build(loader); + cache = newBuilder() + .maximumSize(3000) + .expireAfterAccess(60, TimeUnit.MINUTES) + .softValues() + .build(loader); } @Override diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/CardPanelRenderImpl.java b/Mage.Client/src/main/java/org/mage/card/arcane/CardPanelRenderImpl.java index b90d52cbda..1a714d273e 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/CardPanelRenderImpl.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/CardPanelRenderImpl.java @@ -19,6 +19,7 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; public class CardPanelRenderImpl extends CardPanel { @@ -219,7 +220,12 @@ public class CardPanelRenderImpl extends CardPanel { } // Map of generated images - private final static Cache IMAGE_CACHE = CacheBuilder.newBuilder().softValues().build(); + private final static Cache IMAGE_CACHE = CacheBuilder + .newBuilder() + .maximumSize(3000) + .expireAfterAccess(60, TimeUnit.MINUTES) + .softValues() + .build(); // The art image for the card, loaded in from the disk private BufferedImage artImage;