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 understand the size and reference type of Java objects in JVM

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

Share

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

This article shows you how to understand the size and reference types of Java objects in JVM. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The editor summarizes the size of the Java object in the JVM concept, as well as the definition and distinction of the three reference types.

The size of the type of basic data is fixed, so I won't say much here. For non-basic types of Java objects, their size is open to question.

In Java, the size of an empty Object object is 8byte, which is simply the size of an object in the heap that does not have any properties. Look at the following sentence:

Object ob = new Object ()

This completes the life of a Java object in the program, but the space it occupies is: 4byte+8byte. 4byte is the space needed to hold references in the Java stack mentioned in the above section. And that 8byte is the information about the objects in the Java heap. Because all Java non-primitive objects need to inherit Object objects by default, no matter what kind of Java object they are, they must be larger than 8byte.

With the size of the Object object, we can calculate the size of other objects.

Class NewObject {int count; boolean flag; Object ob;}

Its size is: empty object size (8byte) + int size (4byte) + Boolean size (1byte) + empty Object reference size (4byte) = 17byte. But because Java allocates memory to an object as an integer multiple of 8, the nearest integer multiple of 8 of 17byte is 24, so the size of this object is 24byte.

Java novice advance: elaborate on reference types

Serialization and deserialization of Java objects

Strong, soft, weak, and virtual references to Java objects

Detailed explanation of the concepts of JDK, SDK, JRE and JVM

Four experiences of Java object type conversion

You need to pay attention to the size of the basic types of wrappers here. Because this type of wrapper has become an object, they need to be treated as objects. The size of the wrapper type is at least 12byte (declaring at least the space required for an empty Object), and the 12byte does not contain any valid information, and because the size of the Java object is an integral multiple of 8, the size of a base type wrapper class is at least 16byte. This memory footprint is scary, it is N times that of the basic type (N > 2), and some types of memory footprint is even more exaggerated (just think about it). Therefore, wrapper classes should be used as little as possible. After JDK5.0, because automatic type swapping was added, the Java virtual machine optimized its storage accordingly.

Reference type

Object reference types are divided into strong references, soft references, weak references, and virtual references.

Strong reference: it is the reference generated by the virtual machine when we generally declare that the object is a strong reference. In a strong reference environment, garbage collection needs to strictly determine whether the current object is strongly referenced. If it is strongly referenced, it will not be garbage collected.

Soft references: soft references are generally used as caches. The difference between a soft reference and a strong reference is that when a soft reference is garbage collected, the virtual machine decides whether to reclaim the soft reference based on the remaining memory of the current system. If the remaining memory is tight, the virtual machine reclaims the space referenced by the soft reference; if the remaining memory is relatively rich, it will not be reclaimed. In other words, when an OutOfMemory occurs in a virtual machine, there must be no soft references.

Weak references: weak references are similar to soft references and are used as caches. But unlike soft references, weak references are bound to be recycled when garbage collection is carried out, so their life cycle only exists in one garbage collection cycle.

Strong references needless to say, our systems generally use strong references when using them. "soft reference" and "weak reference" are relatively rare. They are generally used as caches and are generally used as caches when the memory size is limited. Because if the memory is large enough, you can directly use the strong reference as the cache, and it is more controllable. Therefore, what they are common is the cache used in desktop applications.

The above is how to understand the size and reference types of Java objects in JVM. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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