0
嘗試緩存一些MB數量的對象時,我觀察到Ehcache會在保持緩存大小的同時將其大小加倍。Ehcache爲什麼加倍存儲在內存中的字符串的大小?
爲什麼會發生這種情況?這是一種優化嗎?它可以被取消嗎?
以下代碼:
public class Main {
public static void main(String[] args) {
CacheManager manager = CacheManager.newInstance();
Cache oneCache = manager.getCache("OneCache");
String oneMbString = generateDummyString(1024 * 1024);
Element bigElement = new Element("key", oneMbString);
oneCache.put(bigElement);
System.out.println("size: "+ oneCache.getSize());
System.out.println("inMemorySize: " + oneCache.calculateInMemorySize());
System.out.println("size of string: " + oneMbString.getBytes().length);
}
/**
* Generate a dummy string
*
* @param size the size of the string in bytes.
* @return
*/
private static String generateDummyString(int size) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
sb.append("a");
}
return sb.toString();
}
}
將輸出:
大小:1
inMemorySize:串的2097384
尺寸:1048576
PS:本ehcache.xml中文件:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect" maxBytesLocalHeap="512M">
<cache name="OneCache"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LFU">
<sizeOfPolicy maxDepth="10000" maxDepthExceededBehavior="abort"/>
</cache>
</ehcache>
嘗試在generateDummyString函數中使用unicode字符。 – sgmoore
@sgmoore剛剛嘗試過,沒有任何改變。同樣如您所見,我檢查字符串的大小和內存大小,因此如果字節大小以字節爲單位,我會注意到它。 – Flowryn