How much memory does a String take in Java?
Posted on 2004-04-13
I am about to cache a lot of data from a database into a static HashMap and have to estimate how much memory I would chew up if I store it like this.
I know that Java stores characters as Unicode but is there any documentation as to which encoding is used by Java itself? (eg. UTF-8, UTF-16, UTF-32)
So if I store the string "Hello", under UTF-8 I think I chew up 5 bytes (worst case for non-ASCII 20 bytes) and under UTF-16 its 10 bytes (worst case for non-ASCII 20 bytes)