v0.6.3b: fixed memory leaks in android 8.0+, and improved text cache
This commit is contained in:
parent
099a75d19e
commit
c471cc6b96
|
@ -44,45 +44,25 @@ public class RenderedText extends Image {
|
||||||
private static Typeface font;
|
private static Typeface font;
|
||||||
|
|
||||||
//this is basically a LRU cache. capacity is determined by character count, not entry count.
|
//this is basically a LRU cache. capacity is determined by character count, not entry count.
|
||||||
//will attempt to clear oldest, not in use entires until there are 500 characters stored.
|
|
||||||
//FIXME: Caching based on words is very inefficient for every language but chinese.
|
//FIXME: Caching based on words is very inefficient for every language but chinese.
|
||||||
private static LinkedHashMap<String, CachedText> textCache =
|
private static LinkedHashMap<String, CachedText> textCache = new LinkedHashMap<>(700, 0.75f, true);
|
||||||
new LinkedHashMap<String, CachedText>(700, 0.75f, true){
|
|
||||||
private int cachedChars = 0;
|
|
||||||
private final int MAX_CACHED = 1000;
|
|
||||||
|
|
||||||
@Override
|
private static int cachedChars = 0;
|
||||||
public CachedText put(String key, CachedText value) {
|
|
||||||
cachedChars += value.length;
|
|
||||||
CachedText added = super.put(key, value);
|
|
||||||
runGC();
|
|
||||||
return added;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private static final int GC_TRIGGER = 1250;
|
||||||
public CachedText remove(Object key) {
|
private static final int GC_TARGET = 1000;
|
||||||
CachedText removed = super.remove(key);
|
|
||||||
if (removed != null) {
|
|
||||||
cachedChars-= removed.length;
|
|
||||||
removed.texture.delete();
|
|
||||||
}
|
|
||||||
return removed;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private static void runGC(){
|
||||||
public void clear() {
|
Iterator<Map.Entry<String, CachedText>> it = textCache.entrySet().iterator();
|
||||||
super.clear();
|
while (cachedChars > GC_TARGET && it.hasNext()){
|
||||||
cachedChars = 0;
|
CachedText cached = it.next().getValue();
|
||||||
}
|
if (cached.activeTexts.isEmpty()) {
|
||||||
|
cachedChars -= cached.length;
|
||||||
private void runGC(){
|
cached.texture.delete();
|
||||||
Iterator<Map.Entry<String, CachedText>> it = this.entrySet().iterator();
|
it.remove();
|
||||||
while (cachedChars > MAX_CACHED && it.hasNext()){
|
}
|
||||||
CachedText cached = it.next().getValue();
|
}
|
||||||
if (cached.activeTexts.isEmpty()) it.remove();
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private int size;
|
private int size;
|
||||||
private String text;
|
private String text;
|
||||||
|
@ -200,7 +180,13 @@ public class RenderedText extends Image {
|
||||||
r.cache.length = r.text.length();
|
r.cache.length = r.text.length();
|
||||||
r.cache.activeTexts = new HashSet<>();
|
r.cache.activeTexts = new HashSet<>();
|
||||||
r.cache.activeTexts.add(r);
|
r.cache.activeTexts.add(r);
|
||||||
|
|
||||||
|
cachedChars += r.cache.length;
|
||||||
textCache.put("text:" + r.size + " " + r.text, r.cache);
|
textCache.put("text:" + r.size + " " + r.text, r.cache);
|
||||||
|
|
||||||
|
if (cachedChars >= GC_TRIGGER){
|
||||||
|
runGC();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,6 +216,7 @@ public class RenderedText extends Image {
|
||||||
for (CachedText cached : textCache.values()){
|
for (CachedText cached : textCache.values()){
|
||||||
cached.texture.delete();
|
cached.texture.delete();
|
||||||
}
|
}
|
||||||
|
cachedChars = 0;
|
||||||
textCache.clear();
|
textCache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user