[libbluray-devel] Use weak references in native font cache

hpi1 git at videolan.org
Mon Dec 1 09:28:52 CET 2014


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Mon Nov 24 13:32:38 2014 +0200| [f3626ce9647fadfa4aef3c3f6c86f22c7242b16b] | committer: hpi1

Use weak references in native font cache

Allows garbage collector to unload unused fonts.

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=f3626ce9647fadfa4aef3c3f6c86f22c7242b16b
---

 src/libbluray/bdj/java/java/awt/BDFontMetrics.java |   18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
index ba3441c..4c43356 100644
--- a/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
+++ b/src/libbluray/bdj/java/java/awt/BDFontMetrics.java
@@ -20,6 +20,7 @@
 package java.awt;
 
 import java.io.File;
+import java.lang.ref.WeakReference;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
@@ -123,9 +124,12 @@ public class BDFontMetrics extends FontMetrics {
         Iterator it = fontMetricsMap.values().iterator();
         while (it.hasNext()) {
             try {
-                BDFontMetrics fm = (BDFontMetrics)it.next();
+                WeakReference ref = (WeakReference)it.next();
+                BDFontMetrics fm = (BDFontMetrics)ref.get();
                 it.remove();
-                fm.destroy();
+                if (fm != null) {
+                    fm.destroy();
+                }
             } catch (Throwable e) {
                 e.printStackTrace();
             }
@@ -155,10 +159,16 @@ public class BDFontMetrics extends FontMetrics {
                     nativeName = (String)systemFontNameMap.get("default." + font.getStyle());
                 }
             }
+
             String key = nativeName + "." + font.getSize();
-            fm = (BDFontMetrics)fontMetricsMap.get(key);
+            WeakReference ref = (WeakReference)fontMetricsMap.get(key);
+            if (ref != null) {
+                fm = (BDFontMetrics)ref.get();
+            }
+
             if (fm == null) {
-                fontMetricsMap.put(key, fm = new BDFontMetrics(font, nativeName));
+                fm = new BDFontMetrics(font, nativeName);
+                fontMetricsMap.put(key, new WeakReference(fm));
             }
             font.metrics = fm;
         }



More information about the libbluray-devel mailing list