threads / patch / 20556

patchUse SoftReference in WindowCache to keep Repository around

Subject: [JGIT PATCH] Use SoftReference in WindowCache to keep Repository around

## tl;dr

One message between Aug 12, 2009 and Aug 12, 2009. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Shawn O. Pearce· Aug 12, 2009, 17:03 UTC · lore

This is meant to be a memory sensitive cache that holds the Repository object around if there is JVM memory available to store it, and any packs it might have loaded.

Unfortunately I wrote the cache with a WeakReference, which is cleared as soon as there are no more strong references to the Repository. This isn't very suitable for use in a server application or a GUI which is depending upon this cache to hold frequently accessed Repositories for near-term reuse.

Switching these to SoftReference, which is what we also use in the WindowCache, permits the JVM to retain these Repository objects until the JVM is low on memory and the Repository is not strongly held. This new behavior is more consistent with the name and intent of the class.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/lib/RepositoryCache.java  |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
Show changes to org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java +3 −3
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java
index 386f798..3aaffee 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryCache.java
@@ -40,7 +40,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.lang.ref.Reference;
-import java.lang.ref.WeakReference;
+import java.lang.ref.SoftReference;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -156,7 +156,7 @@ private Repository openRepository(final Key location,
 				db = ref != null ? ref.get() : null;
 				if (db == null) {
 					db = location.open(mustExist);
-					ref = new WeakReference<Repository>(db);
+					ref = new SoftReference<Repository>(db);
 					cacheMap.put(location, ref);
 				}
 			}
@@ -167,7 +167,7 @@ private Repository openRepository(final Key location,
 
 	private void registerRepository(final Key location, final Repository db) {
 		db.incrementOpen();
-		WeakReference<Repository> newRef = new WeakReference<Repository>(db);
+		SoftReference<Repository> newRef = new SoftReference<Repository>(db);
 		Reference<Repository> oldRef = cacheMap.put(location, newRef);
 		Repository oldDb = oldRef != null ? oldRef.get() : null;
 		if (oldDb != null)
-- 
1.6.4.225.gb589e

← back to recent threads