The -Xmx sets the total (maximum) heap size. However, a Java heap usually consists of 2 (or more) spaces 1 and the Java object must fit into one space.
When you set the heap to 12mb, none of the spaces is large enough 2 to hold a single ~ 8mb object. When you increase the total heap size to 13 mb, one of the spaces is large enough.
1 - "new" or "eden" space, where usually new objects stand out. An "old" or "permanent" space is the place where objects end after they have survived a certain number of GC cycles. However, really large objects (for example, yours) can be allocated directly to the "old" space. (Note: this is a simplification ...)
2 - You can change the relative size of the new space, but this has other consequences. For example, if you reduce the size of the new space too much, you are likely to increase the percentage of “full” GC ..., which affects both throughput and GC pauses.
source share