duoan / notes

Classtag's Notebooks
https://github.com/classtag/notebook/issues
9 stars 3 forks source link

mapdb调研 #6

Open duoan opened 8 years ago

duoan commented 8 years ago

需要提升下系统的吞吐量,使用基于jvm的ehcache or guava 都不能很好地解决gc old gen的问题。项目中大量使用了ehcache和guava cache 导致项目到一定时间就开始频繁地GC。所以降低jvm 堆内存的使用才是正确的选择,有必要尝试下 mapdb 或者 本机redis 或者是 leveldb,这种java对外内存。提高系统的吞吐量

duoan commented 8 years ago

一般情况下使用DB and DBMaker 就可以满足我们的使用 很多使用都是基于builder模式的,所以使用的时候非常方便,同时DB还可以支持事务(commit rollback)

duoan commented 8 years ago
DB db = DBMaker
        .memoryDB()
        .transactionDisable()
        .closeOnJvmShutdown()
        .make();

会创建一个基于内存的、不支持事务、jvm进程保护(jvm关闭的时候加入hook)的缓存db

duoan commented 8 years ago

4 different storage implementations, commit and disk sync strategies, caches, compressions

这4种不同的mapdb存储实现

duoan commented 8 years ago

默认情况下mapdb make的db是基于 Random-Access-File 的,而不是基于内存的,因为32位机器上内存最大支持4G

duoan commented 8 years ago

64位机器才可以激活 mmap files

DB db = DBMaker
    .fileDB(file)
    .fileMmapEnable()            // always enable mmap
    .fileMmapEnableIfSupported() // only enable on supported platforms
    .fileMmapCleanerHackEnable() // closes file on DB.close()
    .make();

//optionally preload file content into disk cache
Store.forDB(db).fileLoad();
duoan commented 8 years ago

File channel

DB db = DBMaker
    .fileDB(file)
    .fileChannelEnable()
    .make();

注意,这里有一个线程中断的处理

duoan commented 8 years ago

支持异步写Asynchronous write

DB db = DBMaker
    .memoryDB()
    .asyncWriteEnable()
    .asyncWriteQueueSize(10000) //optionally change queue size
    .executorEnable()   //enable background threads to flush data
    .make();
duoan commented 8 years ago

上面3中,第一个中影响GC,第二种不影响GC也是推荐使用的一种,但是需要增大最大堆内存 -Xmx10G

最后一个是直接对外内存 需要设置 -XX:MaxDirectMemorySize=10G,但是这种使用直接内存的方式是有坑的,请google

duoan commented 8 years ago
File file = new File("/tmp/cache");
        DB db = DBMaker.fileDB(file)
                .allocateStartSize(10 * 1024 * 1024 * 1024)  // 10GB
                .allocateIncrement(512 * 1024 * 1024)       // 512MB
                .closeOnJvmShutdown()
                .asyncWriteEnable()
                .transactionDisable()
                .make();

        BTreeMap<String, String> abc = db.treeMap("abc");
        abc.put("a","aaa");
        db.close();
duoan commented 8 years ago

MapDB offers 5 cache implementations. Some are unbounded and could cause OutOfMemoryError when used incorrectly.

5种cache的实现。不能无限制地使用,这样会引起OutOfMemoryError

duoan commented 8 years ago

从mapdb中fetch到的结果数据是immutable的,也就是说,不如果要改变值,必须要先clone一个新的对象。 这就像scala一样

duoan commented 8 years ago

五种缓存实现

duoan commented 8 years ago

关于mapdb的性能测试 http://hill007299.iteye.com/blog/2002835