From 2616b5046d07697bbd04ca462a3c4a63e451a5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Mon, 10 Apr 2023 18:24:32 +0200 Subject: [PATCH] Added #1998 Setting for maximum number of items in the cache - allows less memory consumption (Defaults to 500 per cache) --- CHANGELOG.md | 4 ++ .../flash/configuration/Configuration.java | 4 ++ .../src/com/jpexs/helpers/Cache.java | 42 +++++++++++++++++-- .../locales/AdvancedSettingsDialog.properties | 4 ++ .../AdvancedSettingsDialog_cs.properties | 4 ++ 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index caaadc5d7..f00f7b794 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added +- [#1998] Setting for maximum number of items in the cache - allows less memory consumption (Defaults to 500 per cache) + ### Fixed - [#2004] Freezing when a shape has nonimage character set as fill - [#2004] Nonrepeating fill border @@ -3027,6 +3030,7 @@ All notable changes to this project will be documented in this file. [alpha 9]: https://github.com/jindrapetrik/jpexs-decompiler/compare/alpha8...alpha9 [alpha 8]: https://github.com/jindrapetrik/jpexs-decompiler/compare/alpha7...alpha8 [alpha 7]: https://github.com/jindrapetrik/jpexs-decompiler/releases/tag/alpha7 +[#1998]: https://www.free-decompiler.com/flash/issues/1998 [#2004]: https://www.free-decompiler.com/flash/issues/2004 [#1993]: https://www.free-decompiler.com/flash/issues/1993 [#1994]: https://www.free-decompiler.com/flash/issues/1994 diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java index cce3e6fc2..7cb1a3244 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/configuration/Configuration.java @@ -794,6 +794,10 @@ public final class Configuration { @ConfigurationCategory("limit") public static ConfigurationItem maxCachedTime = null; + @ConfigurationDefaultInt(500) + @ConfigurationCategory("limit") + public static ConfigurationItem maxCachedNum = null; + @ConfigurationDefaultString("") @ConfigurationCategory("paths") @ConfigurationFile(".*\\.swc$") diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/Cache.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/Cache.java index e4fb32b4b..6b868d3b9 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/Cache.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/Cache.java @@ -22,6 +22,8 @@ import java.io.File; import java.io.IOException; import java.lang.ref.WeakReference; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -82,7 +84,7 @@ public class Cache implements Freed { public static Cache getInstance(boolean weak, boolean memoryOnly, String name, boolean temporary) { if (oldCleaner == null) { - oldCleaner = new Thread("Old cache cleaner") { + oldCleaner = new Thread("Cache cleaner") { @Override public void run() { while (!Thread.interrupted()) { @@ -92,7 +94,7 @@ public class Cache implements Freed { return; } try { - clearAllOld(); + clearAllOldAndOverMax(); } catch (Exception cme) { Logger.getLogger(Cache.class.getSimpleName()).log(Level.SEVERE, "Error during clearing cache thread", cme); } @@ -230,6 +232,37 @@ public class Cache implements Freed { return ret; } + private synchronized int clearOverMax() { + Set keys = new HashSet<>(lastAccessed.keySet()); + int num = 0; + + if (Configuration.maxCachedNum.get() > 0 && keys.size() > Configuration.maxCachedNum.get()) + { + List keysList = new ArrayList<>(keys); + Collections.sort(keysList, new Comparator(){ + @Override + public int compare(K o1, K o2) { + long t1 = lastAccessed.get(o1); + long t2 = lastAccessed.get(o2); + if (t1 > t2) { + return 1; + } + if (t2 > t1) { + return -1; + } + return 0; + } + }); + int cnt = keysList.size() - Configuration.maxCachedNum.get(); + for (int i = 0; i < cnt; i++) { + remove(keysList.get(i)); + num++; + } + } + + return num; + } + private synchronized int clearOld() { long currentTime = System.currentTimeMillis(); Set keys = new HashSet<>(lastAccessed.keySet()); @@ -237,7 +270,7 @@ public class Cache implements Freed { if (temporaryThreshold == 0) { return 0; } - int num = 0; + int num = 0; for (K key : keys) { long time = lastAccessed.get(key); if (time < currentTime - temporaryThreshold) { @@ -248,12 +281,13 @@ public class Cache implements Freed { return num; } - private static void clearAllOld() { + private static void clearAllOldAndOverMax() { int num = 0; synchronized (instancesLock) { for (WeakReference cw : instances) { Cache c = cw.get(); if (c != null) { + num += c.clearOverMax(); if (c.temporary) { num += c.clearOld(); } diff --git a/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog.properties b/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog.properties index 2f23b53d0..cc101e136 100644 --- a/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog.properties +++ b/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog.properties @@ -687,3 +687,7 @@ config.description.rememberFoldersScrollPos = Folders scroll position is retaine #after 18.3.6 config.name.warning.initializers.class = Warn on AS3 class trait editation about script initializer config.description.warning.initializers.class = Show warning on AS3 class trait editation about initializer + +#after 18.4.1 +config.name.maxCachedNum = Maximum number of cached items per single cache +config.description.maxCachedNum = Maximum number of cached items before older items are removed from cache. Lower value = less memory, slower app. Higher value = more memory, faster app. Set this to 0 to unlimited caching. diff --git a/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog_cs.properties b/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog_cs.properties index 2e5c6684b..033f12e50 100644 --- a/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog_cs.properties +++ b/src/com/jpexs/decompiler/flash/gui/locales/AdvancedSettingsDialog_cs.properties @@ -677,3 +677,7 @@ config.description.rememberFoldersScrollPos = Pozice odskrolov\u00e1n\u00ed u sl #after 18.3.6 config.name.warning.initializers.class = Varovat p\u0159i AS3 class editaci o inicializ\u00e1toru config.description.warning.initializers.class = Zobrazovat varov\u00e1n\u00ed p\u0159i editaci AS3 class o inicializ\u00e1toru + +#after 18.4.1 +config.name.maxCachedNum = Maxim\u00e1ln\u00ed po\u010det prvk\u016f v jedn\u00e9 cache +config.description.maxCachedNum = Maxim\u00e1ln\u00ed po\u010det cachovan\u00fdch prvk\u016f p\u0159ed t\u00edm, ne\u017e jsou star\u0161\u00ed prvky vymaz\u00e1ny. Ni\u017e\u0161\u00ed hodnota = m\u00e9n\u011b pam\u011bti, pomalej\u0161\u00ed aplikace. Vy\u0161\u0161\u00ed hodnota = v\u00edce pam\u011bti, rychlej\u0161\u00ed aplikace. Nastavte sem hodnotu 0 pro nekone\u010dn\u00e9 cachov\u00e1n\u00ed.