Skip to content

Instantly share code, notes, and snippets.

@jankotek
Created January 12, 2015 15:05
Show Gist options
  • Save jankotek/fbf8f96cb77aebefe4e3 to your computer and use it in GitHub Desktop.
Save jankotek/fbf8f96cb77aebefe4e3 to your computer and use it in GitHub Desktop.
offheap cache
/*
- previous example was using `byte[]` as a backend, so it was failing. This example uses DirectByteBuffer
- it had cache which was consuming a lot of Heap
- set JVM memory limits:
-Xmx64M -XX:MaxDirectMemorySize=2G
- current master branch does not have storage statistics. I did rewrite and its not implemented yet.
*/
package examples;
import org.mapdb.DBMaker;
import org.mapdb.HTreeMap;
import org.mapdb.Store;
import java.util.Random;
/**
* This example shows how-to create off-heap cache,
* where entries expire when maximal store size is reached.
*
* It also shows howto get basic statistics about store size.
*/
public class CacheOffHeap {
public static void main(String[] args) {
final double cacheSizeInGB = 1.0;
// Create cache backed by off-heap store
// In this case store will use ByteBuffers backed by byte[].
HTreeMap cache = DBMaker
.newMemoryDirectDB()
.cacheDisable()
.make()
.createHashMap("test")
.expireStoreSize(cacheSizeInGB)
.make();
// Other alternative is to use Direct ByteBuffers.
// In this case the memory is not released if cache is not correctly closed.
// cache = DBMaker.newCacheDirect(cacheSizeInGB);
//generates random key and values
Random r = new Random();
//used to print store statistics
Store store = Store.forEngine(cache.getEngine());
// insert some stuff in cycle
for(long counter=1; counter<1e8; counter++){
long key = r.nextLong();
byte[] value = new byte[1000];
r.nextBytes(value);
cache.put(key,value);
if(counter%1e5==0){
System.out.printf("Map size: %,d, counter %,d, store size: %,d, store free size: %,d\n",
cache.sizeLong(), counter, store.getCurrSize(), store.getFreeSize());
}
}
// and release memory. Only necessary with `DBMaker.newCacheDirect()`
cache.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment