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

What is the memory consumption of various data types in Java?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the situation of various data types in Java on memory occupation". In daily operation, I believe that many people have doubts about the situation of memory occupation of various data types in Java. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "what is the situation of memory occupation of various data types in Java?" Next, please follow the editor to study!

Preface

Memory formula: memory distribution of Java object = object header (Header) + instance data (Instance Data) + completion fill (Padding).

Make-up padding: the space occupied by Java objects is 8 bytes aligned, that is, the number of bytes occupied by all Java objects must be a multiple of 8.

Shallow Size

The amount of memory consumed by the object itself, excluding the object it references.

For an object of non-array type, its size is the sum of the size of the object and all its member variables. Of course, it will also include some data storage units with java language features.

For an array type object, its size is the sum of the size of the array element object.

Retained Size

Retained Size= current object size + the sum of the sizes of objects to which the current object can be referenced directly or indirectly. (meaning of indirect reference: a-> B-> C, C is indirect reference)

In other words, Retained Size is the total memory that can be freed from the Heap after the current object is GC.

However, objects that are directly or indirectly referenced by GC Roots should also be excluded when released. They will not be regarded as Garbage for the time being.

Basic data types occupy space boolean, byte1byteshort, char2byteint, float4bytelong, double8byte

Next, verify with JProfiler:

1. Create a new empty object and observe the memory consumption of the empty object

Public class TestObject {}

Conclusion: general self-built empty objects occupy 16 b memory 16 = 12 (Header) + 4 (Padding).

two。 Add an int attribute to TestObj to observe the memory consumption of objects

Public class TestObj {private int i;}

Conclusion: int occupies 4b, 16 = 12 (Header) + 4 (int).

3. Add a long attribute to TestObj to observe the memory consumption of objects

Public class TestObj {private long i;}

Conclusion: long occupies 8b, 24 = 12 (Header) + 8 (long) + 4 (Padding).

Other basic types can be self-verified with reference to the above, the principle is the same

Package type occupancy

The amount of memory occupied by the wrapper class (Boolean/Byte/Short/Character/Integer/Long/Double/Float) = the size of the object header + the size of the underlying underlying data type.

Wrapper classes, like other reference classes, produce a reference (reference)

Type occupies space Boolean, Byte16byteShort, Char16byteInteger, Float16byteLong, Double24byte

1. Add an Integer attribute to TestObj to observe the memory consumption of objects

Public class TestObj {private Integer i = 128;}

Object occupies 32b of memory, as shown in the figure

Conclusion: Integer occupies 16b, 32 = 12 (Header) + 16 (Integer) + 4 (reference).

Special:-128: 127 in constant pool, occupies only 4b, and does not generate references (reference)

two。 Add a Long attribute to TestObj to observe the memory consumption of objects

Public class TestObj {private Long l = new Long (1);}

Conclusion: Long occupies 24b, 40 = 12 (Header) + 24 (Long) + 4 (reference).

Other packaging types can be self-verified with reference to the above, the principle is the same

Basic type array occupancy

On a 64-bit machine, the object header of the array object takes 24 bytes and 16 bytes when compression is enabled. It takes up more memory than normal objects because it requires extra space to store the length of the array (normal 16b-12b).

Size of the object array itself = array object header + length * holds the size of a single element

Add a char [] attribute to TestObj to observe the memory consumption of objects

Public class TestObj {private char [] c = {'axiaqingjingjiaoyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanqingzhongyuanzhongyuanzhongyuanzhongyuanzhongjia public class TestObj;}

Conclusion: char [3] occupies 24b, 24 = 40-16, 24 = 16 (Header) + 3 * 2 (char) + 2 (Padding).

Encapsulation type array occupancy

Encapsulating an array of types than an array of basic types requires more references to management elements.

Size of the object array itself = array object header + length * reference pointer size + length * holds the size of a single element

Add an Integer [] attribute to TestObj to observe the memory consumption of objects

Public class TestObj {private Integer [] I = {128129130};}

Conclusion: Integer [3] occupies 80b, 80 = 96-16,80 = 16 (Header) + 3 * 4 (reference) + 3 * 16 (Integer) + 4 (padding).

String takes up memory

Add an empty String attribute in TestObj to observe the memory consumption of the object

Public class TestObj {private String s = new String (");}

Conclusion: String itself occupies 24b, 24 = 40-16, that is to say, empty "" also needs 16b.

Note: why do you write String s = new String ("") here? Please think for yourself. What will happen if you don't write?

A: if you write String s = "", you will not open up memory in the heap, and you will not see the space occupied by String. What you will see will be the following, as to why, it is all because of final.

At this point, the study on "what is the memory occupation of various data types in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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