3 billion data stored in 16G memory (Java Map) reprint

Source: Internet
Author: User

In the discussion of how to weigh, put forward to build btree with direct buffer, think there should be a ready plan, so find a good thing:

mapdb-mapdb:http://www.mapdb.org/

Following from: Kotek.net:http://kotek.net/blog/3g_map

3 billion items in Java Map with GB RAM

One rainy evening I meditated about memory managment in Java and how effectively Java collections utilise memory. I made simple experiment, what much entries can I insert into Java Map with more than GB of RAM?

Goal of this experiment are to investigate internal overhead of collections. So I decided to use small keys and small values. All tests were made on Linux 64bit Kubuntu 12.04. JVM is 64bit Oracle Java with 1.7.0_09-b05 HotSpot 23.5-b02 . There is the option to use compressed pointers (-xx:+usecompressedoops), and which is on by the default on the this JVM.

First is the naive test with java.util.TreeMap . It inserts number into map, until it runs out of the memory and ends with exception. JVM settings for this test was-Xmx15G

import java.util.*; Map m = new TreeMap();for(long counter=0;;counter++){  m.put(counter,"");  if(counter%1000000==0) System.out.println(""+counter);}

This example ended at 172 milion entries. Near the end insertion rate slowed down thanks to excesive GC activity. On second run I replaced TreeMap with ' HashMap, it ended at 182 milions.

Java default collections is not the most memory efficient option. So lets try an memory-optimized. I choosed from LongHashMap mapdb, which uses primitive long keys and are optimized to has small memory footprint. JVM Settings is again-Xmx15G

import org.mapdb.*LongMap m = new LongHashMap();    for(long counter=0;;counter++){  m.put(counter,"");  if(counter%1000000==0) System.out.println(""+counter);}

This time counter stopped at 276 million entries. Again near the end insertion rate slowed down thanks to excesive GC activity.
It looks like this is the limit for heap-based collections, garbage Collection simply brings overhead.

Now was time to pull out the big gun:-). We can always go of-heap where GC can isn't see our data. Let me introduce-mapdb, it provides concurrent TREEMAP and HashMap backed by database engine. It supports various storage modes, one of the them is off-heap memory. (Disclaimer:i am mapdb author).

So lets run previous example, and now with Off-heap Map. First is few lines to configure and open database, it opens Direct-memory store with transactions disabled. Next Line creates new Map within the DB.

import org.mapdb.*DB db = DBMaker   .newDirectMemoryDB()   .transactionDisable()   .make();Map m = db.getTreeMap("test");for(long counter=0;;counter++){  m.put(counter,"");  if(counter%1000000==0) System.out.println(""+counter);}

This is Off-heap Map, so we need different JVM settings: -XX:MaxDirectMemorySize=15G -Xmx128M . This test runs the memory at 980 million records.

But the mapdb can do better. Problem in previous sample is the record fragmentation, b-tree node changes its size on each insert. Workaround is-to-hold b-tree nodes in cache for short moment before they is inserted. This reduces the record fragmentation to minimum. So lets the change DB configuration:

DB db = DBMaker     .newDirectMemoryDB()     .transactionDisable()     .asyncFlushDelay(100)     .make();Map m = db.getTreeMap("test");         

This records runs out of the memory with 1 738 million records. Just amazing 1.7 bilion items is inserted within.

Mapdb can do even better. Lets increase b-tree node size from + to entries and enable transparent compression:

   DB db = DBMaker            .newDirectMemoryDB()            .transactionDisable()            .asyncFlushDelay(100)            .compressionEnable()            .make();   Map m = db.createTreeMap("test",120, false, null, null, null);

This example runs off of memory at whipping 3 315 million records. It is slower thanks to compression, but it still finishes within a few hours. I could probably make some optimization (custom serializers etc) and push number of entries to somewhere around 4 billions .

Maybe wander how all those entries can fit there. Answer is Delta-key compression. Also inserting incremental key (already ordered) into B-tree are best-case scenario and mapdb are slightly optimized for it. Worst case scenario are inserting keys at random order:

UPDATE added latter:there is bit confusion about compression. Delta-key compression is active by default on all examples. In this example I activated aditional zlib style compression.

    DB db = DBMaker            .newDirectMemoryDB()            .transactionDisable()            .asyncFlushDelay(100)            .make();    Map m = db.getTreeMap("test");    Random r = new Random();    for(long counter=0;;counter++){        m.put(r.nextLong(),"");        if(counter%1000000==0) System.out.println(""+counter);    }

But even with the random order mapdb handles to the store 651 million records, nearly 4 times more then heap-based collections.

This little excersice does not has much purpose. It is just one of many I does to optimize mapdb. Perhaps most amazing are that insertion speed were actually wery good and mapdb can compete with memory based collections.

3 billion data stored in 16G memory (Java Map) reprint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.