java object大小_java计算object大小

  • Post author:
  • Post category:java


##maven

com.carrotsearch

java-sizeof

0.0.3

##util

public class SizeOfUtil {

public static enum SizeEnum {

B {

@Override

public double getFromByteSize(long byteSize) {

return byteSize;

}

},K {

@Override

public double getFromByteSize(long byteSize) {

return byteSize*1.0/1024;

}

},M {

@Override

public double getFromByteSize(long byteSize) {

return byteSize*1.0/1024/1024;

}

},G {

@Override

public double getFromByteSize(long byteSize) {

return byteSize*1.0/1024/1024/1024;

}

};

public abstract double getFromByteSize(long byteSize);

}

/**

* 默认

* 返回bytes

* @param o

* @return

*/

public static double getObjectSize(Object o,SizeEnum sizeEnum){

long byteSize = 0;

if(o instanceof Collection>){

byteSize = RamUsageEstimator.sizeOfAll(o);

}else{

byteSize = RamUsageEstimator.sizeOf(o);

}

return sizeEnum.getFromByteSize(byteSize);

}

}

##使用

System.out.println(SizeOfUtil.getObjectSize(map, SizeOfUtil.SizeEnum.K));



版权声明:本文为weixin_33458486原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。