The JVM allocates memory in chunks so it can be performed across threads concurrently. This called the Thread-Local Allocation Buffer.
TLAB - Thread-local allocation buffer. Used to allocate heap space quickly without synchronization. Compiled code has a "fast path" of a few instructions which tries to bump a high-water mark in the current thread's TLAB, successfully allocating an object if the bumped mark falls before a TLAB-specific limit address.
If you turn this off with -XX:-UseTLAB
you will get more accurate memory usage information.
public static void main(String... args) {
for (int i = 0; i < 10; i++) {
long used1 = memoryUsed();
new Object();
long used2 = memoryUsed();
System.out.println(used2 - used1);
}
}
public static long memoryUsed() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
prints with default options
0
0
0
0
0
0
0
0
0
0
with -XX:-UseTLAB
16
16
16
16
16
16
16
16
16
16