Repository navigation
Expand file tree
/
Copy pathCacheOffHeap.java
More file actions
55 lines (39 loc) · 1.55 KB
/
Copy pathCacheOffHeap.java
File metadata and controls
55 lines (39 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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.newCache(cacheSizeInGB);
// 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();
}
}