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;
...
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;
}
}
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 .