分配器往往需要fast allocator。
如果像malloc一样,维护free_list再分配,显然效率很低。
所以有bump-the-pointer机制。
如JVM的eden,要么GC后变空,要么分配时直接往后+size,返回next_free指针,所以不存在维护free_list的问题。
这样,分配速度大大加快,只要判断分配空间没有超过当前limit即可。
C代码
char *next_free; char *heap_limit; void *alloc (unsigned size) { if (next_free + size > heap_limit) /* 1 */ invoke_garbage_collector (); /* 2 */ char *ret = next_free; next_free += size; return ret; }
bump-pointer在tlab上应用:thread local 的内存由于不需要和其他线程交互同步,因此天然可以用bump-pointor。多线程的GC往往都需要实现TLAB,否则无法直接使用bump-pointer的技术。如果使用同步,则时间开销太大。
TLAB在JVM的eden上直接分配。如果TLAB满了,则继续从eden上分配。如果eden满了,则触发minor GC。