-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #434 from tbak/refactorings/string_cache
1.x Cache string values to reduce memory footprint.
- Loading branch information
Showing
7 changed files
with
160 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
eureka-client/src/main/java/com/netflix/discovery/converters/StringCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.netflix.discovery.converters; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
/** | ||
* @author Tomasz Bak | ||
*/ | ||
public class StringCache { | ||
|
||
public static final int LENGTH_LIMIT = 38; | ||
|
||
private final ReadWriteLock lock = new ReentrantReadWriteLock(); | ||
private final Map<String, WeakReference<String>> cache = new WeakHashMap<>(); | ||
private final int lengthLimit; | ||
|
||
public StringCache() { | ||
this(LENGTH_LIMIT); | ||
} | ||
|
||
public StringCache(int lengthLimit) { | ||
this.lengthLimit = lengthLimit; | ||
} | ||
|
||
public String cachedValueOf(final String str) { | ||
if (str != null && (lengthLimit < 0 || str.length() <= lengthLimit)) { | ||
// Return value from cache if available | ||
try { | ||
lock.readLock().lock(); | ||
WeakReference<String> ref = cache.get(str); | ||
if (ref != null) { | ||
return ref.get(); | ||
} | ||
} finally { | ||
lock.readLock().unlock(); | ||
} | ||
|
||
// Update cache with new content | ||
try { | ||
lock.writeLock().lock(); | ||
WeakReference<String> ref = cache.get(str); | ||
if (ref != null) { | ||
return ref.get(); | ||
} | ||
cache.put(str, new WeakReference<String>(str)); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
return str; | ||
} | ||
return str; | ||
} | ||
|
||
public int size() { | ||
try { | ||
lock.readLock().lock(); | ||
return cache.size(); | ||
} finally { | ||
lock.readLock().unlock(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
eureka-client/src/test/java/com/netflix/discovery/converters/StringCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.netflix.discovery.converters; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* @author Tomasz Bak | ||
*/ | ||
public class StringCacheTest { | ||
|
||
public static final int CACHE_SIZE = 100000; | ||
|
||
@Test | ||
public void testVerifyStringsAreGarbageCollectedIfNotReferenced() throws Exception { | ||
StringCache cache = new StringCache(); | ||
for (int i = 0; i < CACHE_SIZE; i++) { | ||
cache.cachedValueOf("id#" + i); | ||
} | ||
gc(); | ||
// Testing GC behavior is unpredictable, so we set here low target level | ||
// The tests run on desktop show that all strings are removed actually. | ||
assertTrue(cache.size() < CACHE_SIZE * 0.1); | ||
} | ||
|
||
public static void gc() { | ||
System.gc(); | ||
System.runFinalization(); | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
// IGNORE | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters