{"thread":{"id":"20556","subject":"[JGIT PATCH] Use SoftReference in WindowCache to keep Repository around","startedAt":"2009-08-12T17:03:04Z","lastAt":"2009-08-12T17:03:04Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"120423","messageId":"1250096584-27641-1-git-send-email-spearce@spearce.org","threadId":"20556","inReplyTo":null,"subject":"[JGIT PATCH] Use SoftReference in WindowCache to keep Repository around","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-08-12T17:03:04Z","receivedAt":"2009-08-12T17:03:04Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This is meant to be a memory sensitive cache that holds the\nRepository object around if there is JVM memory available to\nstore it, and any packs it might have loaded.\n\nUnfortunately I wrote the cache with a WeakReference, which\nis cleared as soon as there are no more strong references to\nthe Repository.  This isn't very suitable for use in a server\napplication or a GUI which is depending upon this cache to hold\nfrequently accessed Repositories for near-term reuse.\n\nSwitching these to SoftReference, which is what we also use in the\nWindowCache, permits the JVM to retain these Repository objects until\nthe JVM is low on memory and the Repository is not strongly held.\nThis new behavior is more consistent with the name and intent of\nthe class.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../src/org/spearce/jgit/lib/RepositoryCache.java  |    6 +++---\n 1 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java\nindex 386f798..3aaffee 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java\n@@ -40,7 +40,7 @@\n import java.io.File;\n import java.io.IOException;\n import java.lang.ref.Reference;\n-import java.lang.ref.WeakReference;\n+import java.lang.ref.SoftReference;\n import java.util.Iterator;\n import java.util.Map;\n import java.util.concurrent.ConcurrentHashMap;\n@@ -156,7 +156,7 @@ private Repository openRepository(final Key location,\n \t\t\t\tdb = ref != null ? ref.get() : null;\n \t\t\t\tif (db == null) {\n \t\t\t\t\tdb = location.open(mustExist);\n-\t\t\t\t\tref = new WeakReference<Repository>(db);\n+\t\t\t\t\tref = new SoftReference<Repository>(db);\n \t\t\t\t\tcacheMap.put(location, ref);\n \t\t\t\t}\n \t\t\t}\n@@ -167,7 +167,7 @@ private Repository openRepository(final Key location,\n \n \tprivate void registerRepository(final Key location, final Repository db) {\n \t\tdb.incrementOpen();\n-\t\tWeakReference<Repository> newRef = new WeakReference<Repository>(db);\n+\t\tSoftReference<Repository> newRef = new SoftReference<Repository>(db);\n \t\tReference<Repository> oldRef = cacheMap.put(location, newRef);\n \t\tRepository oldDb = oldRef != null ? oldRef.get() : null;\n \t\tif (oldDb != null)\n-- \n1.6.4.225.gb589e\n"}]}