java查看对象占用内存的大小

1.首先引入jar包 lucene-core-4.7.2.jar

    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>

2.java代码示例如下

for (int i = 0; i <100000; i++) {
            TravelAccountBillInfo info = new TravelAccountBillInfo();
            billInfoList.add(info);
            logVoucherError(workLogsPath,"添加第"+i+"个对象,集合大小="+billInfoList.size());
            logVoucherError(workLogsPath,"内存占用大小="+RamUsageEstimator.humanSizeOf(billInfoList));
        }

我们这里把日志记录在一个txt里面了,这里的 humanSizeOf大小打印出来的会自动进行转换成KB,MB

 

 

posted @ 2023-04-17 17:27  凉了记忆  阅读(1434)  评论(0)    收藏  举报