Calculating B-Tree Memory Usage in Java

I implemented a simple B-Tree that displays longs for ints. Now I wanted to evaluate the memory usage using the following method (applies only to 32-bit JVM):

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;
            for (int i = 0; i < 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 I tried this, for example. for 7mio records and the memoryUsage method reports much higher values ​​than the -Xmx option allows! For instance. he says 1040 (MB) and I installed -Xmx300! Is the JVM somehow capable of optimizing the memory layout, for example. for empty arrays or what could be my mistake?

Update1: , isLeaf boolean , , , Xmx. ( , isLeaf == false )

Update2: , - . , ( ), ( btree ). memoryUsage , 500 100 .

+5
1

, ... ;)

, . checkSplitEntry ( ) :

// left child: just copy pointer and decrease size to index
BTreeEntry newLeftChild = this;
newLeftChild.entrySize = splitIndex;

, . , memoryUsage (, !). , , B-Tree , !

0

All Articles