maven
<!-- size of-->
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>java-sizeof</artifactId>
<version>0.0.3</version>
</dependency>
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));
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。