对象分析工具 用于java 对象内存占用分析
<!-- 对象分析工具 1 -->
<!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.15</version>
<scope>provided</scope>
</dependency>
<!-- 对象分析工具 2 https://mvnrepository.com/artifact/com.carrotsearch/java-sizeof -->
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>java-sizeof</artifactId>
<version>0.0.5</version>
</dependency>
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
import java.util.HashMap;
import java.util.Map;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
import com.carrotsearch.sizeof.RamUsageEstimator;
public class MemoryAnalysisDemo {
public static void main(String[] args) {
Map<String, String> phonesMap = new HashMap<>(100000);
for (int i = 0; i < 100000; i++) {
phonesMap.put(String.valueOf(i), "p" + i);
}
// 查看对象内部信息
System.out.println(ClassLayout.parseInstance(phonesMap).toPrintable());
// 查看对象外部信息:包括引用的对象
// System.out.println(GraphLayout.parseInstance(phonesMap).toPrintable());
// 查看对象占用空间总大小
System.out.println("内存占用byte:" + GraphLayout.parseInstance(phonesMap).totalSize());
System.out.println("内存占用byte:" + RamUsageEstimator.sizeOf(phonesMap));
System.out.println("内存占用KB:" + RamUsageEstimator.humanSizeOf(phonesMap));
}
}