Calculating memory usage of a B-Tree in Java -
i've implemented simple b-tree whichs maps longs ints. wanted estimate memory usage of using following method (applies 32bit jvm only):
class btreeentry { int entrysize; long keys[]; int values[]; btreeentry children[]; boolean isleaf; ... /** @return used bytes */ long capacity() { long cap = keys.length * (8 + 4) + 3 * 12 + 4 + 1; if (!isleaf) { cap += children.length * 4; (int = 0; < children.length; i++) { if (children[i] != null) cap += children[i].capacity(); } } return cap; } } /** @return memory usage in mb */ public int memoryusage() { return math.round(rootentry.capacity() / (1 << 20)); }
but tried e.g. 7mio entries , memoryusage method reports higher values -xmx setting allow! e.g. says 1040 (mb) , set -xmx300! jvm somehow able optimize memory layout, eg. empty arrays or mistake?
update1: ok, introducing isleaf boolean reduces memory usage lot, still unclear why observed higher values xmx. (you can still try out via using isleaf == false contructors)
update2: hmmh, wrong. when increasing entries per leaf 1 assume memory usage decreases (when doing compact both), because less overhead of references involved larger arrays (and btree has smaller height). method memoryusage reports increased value if use 500 instead 100 entries per leaf.
ohh sh... bit fresh air solved issue ;)
when entry full splitted. in original split method checksplitentry
(where wanted avoid waste of memory) made big memory waste mistake:
// left child: copy pointer , decrease size index btreeentry newleftchild = this; newleftchild.entrysize = splitindex;
the problem here is, old children pointers still accessible. , so, in memoryusage method i'm counting children twice (especially when did not compact!). so, without trick should fine , b-tree more memory efficient garbage collector can work!
Comments
Post a Comment