最近测试Java性能的时候,需要统计Java对象的大小。以往,可能要自己写工具类,通过GC前后内存占用的大小来计算出Java对象的大小。现在,有一款叫做java-sizeof的插件,可以方便实现这个功能。
java-sizeof 比较老了,最近的更新还停留在2015年,但丝毫不影响咱们使用。在Maven里面引入以下依赖来使用java-sizeof 。
<project
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'>
<modelVersion>4.0.0</modelVersion>
<groupId>com.waylau.javasizeof</groupId>
<artifactId>my-java-sizeof-demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>my-java-sizeof-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<java.version>1.8</java.version>
<junit.version>5.3.2</junit.version>
<java-sizeof.version>0.0.5</java-sizeof.version>
</properties>
<dependencies>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>java-sizeof</artifactId>
<version>${java-sizeof.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 设置编译插件 -->
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source> <!-- 源代码使用的开发版本 -->
<target>${java.version}</target> <!-- 需要生成的目标class文件的编译版本 -->
</configuration>
</plugin>
</plugins>
</build>
</project>以下是我在单元测试用中使用java-sizeof 的案例。用于统计不同数量级别的Java对象占用的空间:
package com.waylau.javasizeof;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.carrotsearch.sizeof.RamUsageEstimator;
/**
* JavaSizeOf Test
*
* @author lWX483509
* @since 2019年11月19日
*/
public class JavaSizeOfTest {
private static final String DATE_STRING = 'abcdefghij1224567890';
private static final Long DATA_LONG = 1224567890L;
private static final Double DATA_DOUBLE = 1224567890D;
private static final LocalDate DATA_DATE = LocalDate.now();
private CpePeggingVO newCpePeggingVO(int i) {
CpePeggingVO peggingVO = new CpePeggingVO();
peggingVO.setDemandId(Long.valueOf(i));
peggingVO.setDemandName(DATE_STRING);
peggingVO.setDemandType(DATE_STRING);
peggingVO.setRootDemandName(DATE_STRING);
peggingVO.setDemandPriority(DATA_DOUBLE);
peggingVO.setShipmentCode(DATE_STRING);
peggingVO.setItemCode(DATE_STRING);
peggingVO.setDemandDate(DATA_DATE);
peggingVO.setDemandQty(DATA_DOUBLE);
peggingVO.setSite(DATE_STRING);
peggingVO.setCountry(DATE_STRING);
peggingVO.setRepoff(DATE_STRING);
peggingVO.setRegion(DATE_STRING);
peggingVO.setMaxBomLevel(1);
peggingVO.setSupplyId(Long.valueOf(i));
peggingVO.setSupplyName(DATE_STRING);
peggingVO.setAvailableDate(DATA_DATE);
peggingVO.setSupplyType(DATE_STRING);
peggingVO.setSupplyLocator(DATE_STRING);
peggingVO.setSupplySource(DATE_STRING);
peggingVO.setSupplyQty(DATA_DOUBLE);
peggingVO.setSupplyPriority(DATA_LONG);
peggingVO.setQtyFed(DATA_DOUBLE);
peggingVO.setBalance(DATA_DOUBLE);
peggingVO.setBatchId(DATA_LONG);
return peggingVO;
}
private List<CpePeggingVO> createPeggingList(int num) {
List<CpePeggingVO> list = new ArrayList<CpePeggingVO>(num);
for (int i = 1; i <= num; i++) {
CpePeggingVO vo = newCpePeggingVO(i);
list.add(vo);
}
return list;
}
@Test
void testHumanSizeOf() {
int n = 1;
String humanSizeOf = RamUsageEstimator.humanSizeOf(newCpePeggingVO(n));
System.out.println(String.format('CpePeggingVO size: %s', humanSizeOf));
humanSizeOf = RamUsageEstimator.humanSizeOf(createPeggingList(n));
System.out.println(String.format('CpePeggingVO list num: %d, size: %s', n, humanSizeOf));
n = 1000;
humanSizeOf = RamUsageEstimator.humanSizeOf(createPeggingList(n));
System.out.println(String.format('CpePeggingVO list num: %d, size: %s', n, humanSizeOf));
n = 10000;
humanSizeOf = RamUsageEstimator.humanSizeOf(createPeggingList(n));
System.out.println(String.format('CpePeggingVO list num: %d, size: %s', n, humanSizeOf));
n = 100000;
humanSizeOf = RamUsageEstimator.humanSizeOf(createPeggingList(n));
System.out.println(String.format('CpePeggingVO list num: %d, size: %s', n, humanSizeOf));
n = 1000000;
humanSizeOf = RamUsageEstimator.humanSizeOf(createPeggingList(n));
System.out.println(String.format('CpePeggingVO list num: %d, size: %s', n, humanSizeOf));
n = 10000000;
humanSizeOf = RamUsageEstimator.humanSizeOf(createPeggingList(n));
System.out.println(String.format('CpePeggingVO list num: %d, size: %s', n, humanSizeOf));
}
}执行测试用例,输出如下:
CpePeggingVO size: 312 bytes CpePeggingVO list num: 1, size: 360 bytes CpePeggingVO list num: 1000, size: 165.2 KB CpePeggingVO list num: 10000, size: 1.6 MB CpePeggingVO list num: 100000, size: 16.4 MB CpePeggingVO list num: 1000000, size: 164 MB CpePeggingVO list num: 10000000, size: 1.6 GB
可以说,统计结果非常直观。通过上述用例,可以得出,List结构,占用了48bytes的空间。






































苏公安网备 32011402010933号