Skip to content

Instantly share code, notes, and snippets.

@arogulin
Last active June 19, 2018 14:48
Show Gist options
  • Save arogulin/d2875d6f8bcba4de8b95ecbda5e2d46a to your computer and use it in GitHub Desktop.
Save arogulin/d2875d6f8bcba4de8b95ecbda5e2d46a to your computer and use it in GitHub Desktop.
Java 8 memory notes
On 64-bit JVM all objects will require a multiple of 8 bytes: 8, 16, 24, 32, 48, 56, 64, etc.
Empty String: 24 bytes
String itself:
24 bytes = 8 bytes "headers" + (4 bytes "size" + 4 bytes "hash" - not anymore since Java 8) + 4 bytes "value reference" + 4 bytes "padding to make 20 to become multiple of 8".
char[0]:
0 bytes = does not cost any memory for this string, because it's shared empty char array, used by all empty strings.
String with 3 chars: 48 bytes
String itself:
24 bytes = 8 bytes "headers" + 4 bytes "size" + 4 bytes "hash" + 4 bytes "value reference" + 4 bytes "padding to make 20 to become multiple of 8".
char[3]:
24 bytes = 12 bytes "headers" + 4 bytes "size" + 6 bytes "3 chars" + 2 bytes "padding to make 22 to become multiple of 8"
String with 4 chars: 48 bytes
String itself:
24 bytes = 8 bytes "headers" + 4 bytes "size" + 4 bytes "hash" + 4 bytes "value reference" + 4 bytes "padding to make 20 to become multiple of 8".
char[4]:
24 bytes = 12 bytes "headers" + 4 bytes "size" + 8 bytes "4 chars"
String with 5 chars: 56 bytes
String itself:
24 bytes = 8 bytes "headers" + 4 bytes "size" + 4 bytes "hash" + 4 bytes "value reference" + 4 bytes "padding to make 20 to become multiple of 8".
char[5]:
32 bytes = 12 bytes "headers" + 4 bytes "size" + 10 bytes "5 chars" + 6 bytes "padding to make 50 to become multiple of 8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment