In most distributed computing architecture, we can not avoid programming directly with byte information. Like Serialization/DeSerialization, File reading/changing, etc. So all of us need some solution resolve the memory allocation/deallocation effiently with good performance numbers and acceptable overhead.


So here I introduce the memory pool design. It's underlying depends on the ByteBuff of java nio.




1. Provide the estimated memory block consumptions.

    We assumed all of the request for memory requirement can be categorized by many blocks[segment]. The segment could like 64 bytes, 128 bytes, 256 bytes, 512 bytes, etc. And then we could define the pool initial size and final size, like init=2 * 1024 bytes and end= 16 * 1024 bytes. And pool will dispatch the request of memory allocation to the segment that can fullfil the request.


     For examples:

     The segment will be 2*1024, 4*1024, 8*1024, 16*1024


     The request need allocate 3456 bytes, so the segment contain 4*1024 can fullfil the request.


2. Direct ByteBuffer or Heap ByteBuffer

    I decided support both of them, as I thought the decision by the application designer. Due to the direct bytebuffer allocation slow than heap bytebuffer, but read/write against direct bytebuffer better than read/write against heap bytebuffer - Of course, all of those comments means generally in average situations.

    Btw, if you decide using the direct bytebuffer. Please remember set pool initialization as earlier as possbile than your application codes.




We already created one open source project: TinyBang - http://code.google.com/p/tinybang/

You can find the memory pool implementation from there.

svn path: https://tinybang.googlecode.com/svn/trunk/src/main/java/memorypool


Configuration items:

1. memory.pool.segment.min.size - the minimum segement size with default value is 2

2. memory.pool.segment.max.size - the maximum segment size with default value is 16

3. memory.pool.segment.size.scale - the scale for size of memory with default value is 1024

(Note: so the pool default start from 2 * 1024 to 16 * 1024)

4. memory.pool.segment.size.inc.pace - the pace for size increasing with default value is 2

(Note: so the pool default range is 2, 4, 6, 8, 16)

5. memory.pool.each.segment.counts - the number of entry was contained within each segment with default value is 50

6. memory.pool.allocate.offjvmheap - Direct bytebuffer with true flag and heap bytebuffer with false flag.