Bean对象占内存大还是将Bean转成字符串后占内存大

2019-01-15  本文已影响22人  不知名的蛋挞
public class RamTest {

    @Test
    public void testRam(){
        User u1 = new User(1,"Amy","girl",false);
        String u2 = JSON.toJSONString(u1);
        System.out.println("u1对象大小为:"+RamUsageEstimator.humanSizeOf(u1));
        System.out.println("u2对象大小为:"+RamUsageEstimator.humanSizeOf(u2));
    }

    class User{
        Integer id;
        String name;
        String sex;
        Boolean isMarried;

        public User(Integer id, String name, String sex, Boolean isMarried) {
            this.id = id;
            this.name = name;
            this.sex = sex;
            this.isMarried = isMarried;
        }
    }
}

输出结果:

u1对象大小为:176 bytes
u2对象大小为:48 bytes

但是将对象转化成String也是需要耗时的,空间和时间之间的冲突需要考虑。

上一篇下一篇

猜你喜欢

热点阅读