* Performance: improved memory usage for images (now xmage will not eat all available memory on 2GB+ settings, see #6375);

This commit is contained in:
Oleg Agafonov 2020-09-09 16:47:45 +04:00
parent 233e12873e
commit cb2ae0295f
2 changed files with 18 additions and 7 deletions

View file

@ -1,23 +1,28 @@
package mage.client.util; 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.base.Function;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.ForwardingLoadingCache; import com.google.common.cache.ForwardingLoadingCache;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import org.apache.log4j.Logger; 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<K, V> extends ForwardingLoadingCache<K, Optional<V>> { public class SoftValuesLoadingCache<K, V> extends ForwardingLoadingCache<K, Optional<V>> {
private final LoadingCache<K, Optional<V>> cache; private final LoadingCache<K, Optional<V>> cache;
private static final Logger logger = Logger.getLogger(SoftValuesLoadingCache.class); private static final Logger logger = Logger.getLogger(SoftValuesLoadingCache.class);
public SoftValuesLoadingCache(CacheLoader<K, Optional<V>> loader) { public SoftValuesLoadingCache(CacheLoader<K, Optional<V>> loader) {
cache = newBuilder().softValues().build(loader); cache = newBuilder()
.maximumSize(3000)
.expireAfterAccess(60, TimeUnit.MINUTES)
.softValues()
.build(loader);
} }
@Override @Override

View file

@ -19,6 +19,7 @@ import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class CardPanelRenderImpl extends CardPanel { public class CardPanelRenderImpl extends CardPanel {
@ -219,7 +220,12 @@ public class CardPanelRenderImpl extends CardPanel {
} }
// Map of generated images // Map of generated images
private final static Cache<ImageKey, BufferedImage> IMAGE_CACHE = CacheBuilder.newBuilder().softValues().build(); private final static Cache<ImageKey, BufferedImage> IMAGE_CACHE = CacheBuilder
.newBuilder()
.maximumSize(3000)
.expireAfterAccess(60, TimeUnit.MINUTES)
.softValues()
.build();
// The art image for the card, loaded in from the disk // The art image for the card, loaded in from the disk
private BufferedImage artImage; private BufferedImage artImage;