How much memory does a string use in Java 8?

前端 未结 4 1951
暖寄归人
暖寄归人 2020-11-30 03:18

I read a lot about memory allocation for Strings lately and can\'t find any details if things are the same with Java 8.

How much memory space would a String like

4条回答
  •  醉酒成梦
    2020-11-30 03:37

    "Alexandru Tanasescu" uses 104 bytes. This is how to get the size

        long m0 = Runtime.getRuntime().freeMemory();
        String s = new String("Alexandru Tanasescu");
        long m1 = Runtime.getRuntime().freeMemory();
        System.out.println(m0 - m1);
    

    Note: run it with -XX:-UseTLAB option

提交回复
热议问题