Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JOL to measure the real memory size of an object

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "how to use JOL to measure the real memory size of the object", the content is detailed, the steps are clear, the details are handled properly, I hope this "how to use JOL to measure the real memory size of the object" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

This paper introduces a tool (memory-measurer) that can easily measure the real memory occupied by an object, such as having such a User object.

Public class User {private Integer id; private String mobile; private Date createTime;}

First look at the memory usage of an empty User object

User u = new User (); System.out.println (MemoryMeasurer.measureBytes (u)); / / 24 System.out.println (ObjectGraphMeasurer.measure (u)); / / Footprint {Objects=1, References=3, Primitives= []}

It is known that the three references of an object occupy a total of 24 bytes.

How much memory is used after assigning values one by one?

/ / assign id Integer id = new Integer (1); System.out.println (MemoryMeasurer.measureBytes (id)); / / 16 u.setId (id); System.out.println (MemoryMeasurer.measureBytes (u)); / / 40 System.out.println (ObjectGraphMeasurer.measure (u)); / / Footprint {Objects=2, References=3, Primitives= [int]}

An Integer object takes up 16 bytes, so after assigning a value to id, the user object becomes 24 bytes 16 bytes 40 bytes.

/ / assign mobile String mobile = "13600000001"; System.out.println (MemoryMeasurer.measureBytes (mobile)); / / 64 u.setMobile (mobile); System.out.println (MemoryMeasurer.measureBytes (u)); / / 104 System.out.println (ObjectGraphMeasurer.measure (u)); / / Footprint {Objects=4, References=4, Primitives= [int x 2, char x 11]}

An 11-bit long mobile string object takes up 64 bytes, so the user object becomes 40 "64" 104 bytes.

/ / assign createTime Date createTime = new Date (); System.out.println (MemoryMeasurer.measureBytes (createTime)); / / 24-byte u.setCreateTime (createTime); System.out.println (MemoryMeasurer.measureBytes (u)); / / 128System.out.println (ObjectGraphMeasurer.measure (u)); / / Footprint {Objects=5, References=5, Primitives= [int x 2, long, char x 11]}

You know that a Date object takes up 24 bytes, so a User object whose properties are not empty takes up 128bytes of memory.

In addition, more detailed Footprint information can be obtained through another tool-- JOL (Java Object Layout).

From the tools above, we only know that an empty User object occupies 24 bytes and a simple

Footprint {Objects=1, References=3, Primitives= []}

Through this tool, we can know how these 24 bytes are allocated.

System.out.println (ClassLayout.parseClass (User.class). ToPrintable () Memorymeasurer.User object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N Instance size A 12 4 Integer User.id N Acer A 16 4 String User.mobile N mer A 20 4 Date User.createTime N Acer An Instance size: 24 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

Above, we know that an Integer object occupies 16 bytes. See how these 16 bytes are allocated.

System.out.println (ClassLayout.parseClass (Integer.class). ToPrintable ()); java.lang.Integer object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N int Integer.value A 12 4 int Integer.value N bytes total An Instance size: 16 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

Above we know that an 11-bit long String object takes up 64 bytes to see how it is allocated

System.out.println (ClassLayout.parseClass (String.class). ToPrintable () Java.lang.String object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) Nash A 12 4 char [] String.value N Acer A 16 4 int String.hash N Acer A 20 4 (loss due to the next object alignment) Instance size: 24 Bytes Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

That is, an empty String object occupies 24 bytes

System.out.println (ClassLayout.parseClass (char [] .class). ToPrintable ()); [C object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) Nash A 160 char [C. N bytes Space losses An Instance size: 16 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

A char array of length 0 takes up 16 bytes, so an 11-bit char array takes up 16 bytes: 16'2'11'38 because it needs to be aligned by 8 bytes, so it has to add 2 bytes of fillers to 40 bytes. So the occupied byte of an 11-bit long string is 24'40'64.

Supplement

How to use memory-measurer

Git clone https://github.com/msteindorfer/memory-measurer cd memory-measurer mvn clean install

Add dependencies to the pom file

Com.github.msteindorfer memory-measurer 0.1.0-SNAPSHOT

Explicitly add vm parameters at run time, such as

-javaagent:/Users/zhugw/workspace/memory-measurer/target/memory-measurer-0.1.0-SNAPSHOT.jar

Instructions for using jol

Just add dependencies.

Org.openjdk.jol jol-core 0.6 read here, this article "how to use JOL to measure the real memory size of objects" article has been introduced, want to master the knowledge of this article still need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report