Added #1998 Setting for maximum number of items in the cache - allows less memory consumption (Defaults to 500 per cache)

This commit is contained in:
Jindra Petřík
2023-04-10 18:24:32 +02:00
parent 49abcd4447
commit 2616b5046d
5 changed files with 54 additions and 4 deletions

View File

@@ -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

View File

@@ -794,6 +794,10 @@ public final class Configuration {
@ConfigurationCategory("limit")
public static ConfigurationItem<Integer> maxCachedTime = null;
@ConfigurationDefaultInt(500)
@ConfigurationCategory("limit")
public static ConfigurationItem<Integer> maxCachedNum = null;
@ConfigurationDefaultString("")
@ConfigurationCategory("paths")
@ConfigurationFile(".*\\.swc$")

View File

@@ -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<K, V> implements Freed {
public static <K, V> Cache<K, V> 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<K, V> 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<K, V> implements Freed {
return ret;
}
private synchronized int clearOverMax() {
Set<K> keys = new HashSet<>(lastAccessed.keySet());
int num = 0;
if (Configuration.maxCachedNum.get() > 0 && keys.size() > Configuration.maxCachedNum.get())
{
List<K> keysList = new ArrayList<>(keys);
Collections.sort(keysList, new Comparator<K>(){
@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<K> keys = new HashSet<>(lastAccessed.keySet());
@@ -237,7 +270,7 @@ public class Cache<K, V> 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<K, V> implements Freed {
return num;
}
private static void clearAllOld() {
private static void clearAllOldAndOverMax() {
int num = 0;
synchronized (instancesLock) {
for (WeakReference<Cache> cw : instances) {
Cache c = cw.get();
if (c != null) {
num += c.clearOverMax();
if (c.temporary) {
num += c.clearOld();
}

View File

@@ -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.

View File

@@ -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.