Reference
https://docs.oracle.com/javase/8/docs/api/java/nio/MappedByteBuffer.html
https://github.com/shooeugenesea/study-practice/tree/ohc
https://github.com/shooeugenesea/study-practice/tree/ohc
What
OHC 是一個 off heap 的 cache library
Why
如果是用 map 做 cache, 資料都在 heap 裡面, heap 裡面的問題就是
- 會被 GC, GC 就會週期性的對 JVM 效能產生影響
- 如果要 persist 需要另外實作
如果是放在 off heap, 比方說用 mmap, 或是用 MappedByteBuffer access 的記憶體, 就是放在 heap memory 之外, 由作業系統控制著.
好處是記憶體量不用被 heap size 限制, 缺點是要自己小心控制, 不然會造成系統問題.'
How
Maven
<dependency> <groupId>org.caffinitas.ohc</groupId> <artifactId>ohc-core</artifactId> <version>0.4.4</version> </dependency>
Codes
public class SimpleMain {
public static void main(String[] params) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()) {
if (scanner.nextLine().trim().equals("GO")) {
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "/" + Runtime.getRuntime().totalMemory()/1024/1024);
OHCache<String,String> c = OHCacheBuilder.<String,String>newBuilder()
.keySerializer(new StringSerializer())
.valueSerializer(new StringSerializer())
.build();
c.put("A", "A");
System.out.println(c.get("A"));
IntStream.range(0, 1000_000).forEach(idx -> {
c.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
});
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "/" + Runtime.getRuntime().totalMemory()/1024/1024);
} else {
System.out.println("input 'GO' then click enter");
}
}
}
private static class StringSerializer implements CacheSerializer<String> {
@Override
public void serialize(String s, ByteBuffer byteBuffer) {
byteBuffer.put(s.getBytes());
}
@Override
public String deserialize(ByteBuffer byteBuffer) {
return StandardCharsets.UTF_8.decode(byteBuffer).toString();
}
@Override
public int serializedSize(String s) {
return s.getBytes().length;
}
}
}
Output
jconsole 與系統資源使用圖可以看到 heap 只用不到 100MB, 但實際上已經用了 600MB.
就是都放在 off heap 中


沒有留言:
張貼留言