这很容易
JOL
(但我不完全确定这是你想要的):
String left = "Jörn";
System.out.println(GraphLayout.parseInstance(left).totalSize()); // 48 bytes
String right = "foobar";
System.out.println(GraphLayout.parseInstance(right).totalSize()); // 48 bytes
String oneMore = "\u03b1";
System.out.println(GraphLayout.parseInstance(oneMore).totalSize()); // 48 bytes
对于编码来说,没有公共的API,但是你可以推断它…
private static String encoding(String s) {
char[] arr = s.toCharArray();
for (char c : arr) {
if (c >>> 8 != 0) {
return "UTF16";
}
}
return "Latin1";
}