git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 12, 2009, 17:03 UTC
Message-ID
<1250096584-27641-1-git-send-email-spearce@spearce.org>

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(-)
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
Message 1 of 1 in “Use SoftReference in WindowCache to keep Repository around”
  1. Use SoftReference in WindowCache to keep Repository aroundShawn O. Pearce, Aug 12, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.