Last active
June 19, 2018 14:48
-
-
Save arogulin/d2875d6f8bcba4de8b95ecbda5e2d46a to your computer and use it in GitHub Desktop.
Java 8 memory notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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