I had a whim recently and suddenly became interested in the memory size of Java objects. I went to collect some information on the Internet and sorted it out. I hope you can help me.
If: you can calculate the memory size occupied by the object new String ("abc") in the JVM (compressed size 48B, uncompressed size 64B in 64-bit JDK7), then you can end it here~
Memory layout of Java objects: Object header (Header), instance data (Instance Data) and alignment padding (Padding) ## #. The object header of the virtual machine includes two parts of information. The first part is used to
store the runtime data of the object itself , such as hashCode, GC generation age, lock status flag, lock held by the thread, and bias Thread ID, bias timestamp, etc. The length of this part of data is 4B and 8B respectively in 32-bit and 64-bit virtual machines (pointer compression is not turned on). It is officially called "Mark Word". The other part of the object is the
type pointer (klass) , which is the pointer of the object to its class metadata. The virtual machine uses this pointer to determine which class the object is an instance of. In addition, if the object is a Java array, there must be a piece of data in the object header to record the length of the array, because the virtual machine can determine the size of the Java object through the metadata information of ordinary Java objects, but from the metadata of the array But cannot determine the size of the array.
The object header occupies 8B on 32-bit systems and 16B on 64-bit systems. Whether it is a 32-bit system or a 64-bit system, objects are 8-byte aligned. When Java turns on pointer compression in 64-bit mode, the header will be 4B larger than in 32-bit mode (the mark area is shifted by 8B, and the kclass area is compressed). If pointer compression is not turned on, the header will be 8B larger (both mark and kclass are compressed). is 8B) , in other words, HotSpot's alignment is 8-byte alignment: (object header + instance data + padding)%8 is equal to 0 and 0
As mentioned in Reference 2, the java.lang.instrument.Instrumentation provided after JDK5 provides a rich API for tracking various aspects of structure and measuring object size. However, this thing requires the use of java agent. As for the agent and Instrumentation, I will not explain them here. I will only explain how to use them.
This class is provided in Reference Material 3. I personally think it is very practical. The code is shown in Appendix 1 below (the code is relatively long, so I will simply put it at the end of the article):
This code can be copied directly, and then Pack it into a jar package (named agent.jar. If the packaging is not successful, you can directly download the package packaged by the blogger). Pay attention to adding a line in META-INF/MANIFEST.MF:
Premain-Class: com.zzh.size.MySizeOf (注意":"后面的空格,否则会报错:invalid header field.)Give an example Case, the code is as follows (the blogger’s system is 64-bit and uses 64-bit JDK7):
import com.zzh.size.MySizeOf;public class ObjectSize { public static void main(String args[]) { System.out.println(MySizeOf.sizeOf(new Object())); } }Next, compile and run, the steps are as follows:
- Compile (agent.jar is placed in the current directory): javac -classpath agent.jar ObjectSize.java
- Run: java -javaagent:agent.jar ObjectSize (output result: 16, As for the analysis of this result, we will elaborate on it later)
Using Instrumentation to test the size of an object is just to express the size of an object more vividly. In fact, when an object is created, its size can be calculated manually. Code case practice is used to prove the rationality and correctness of theoretical knowledge. , the specific algorithm is reflected in the code case below.
Memory Required(bytes) | |
---|---|
1 | |
1 | |
2 | |
2 | |
4 | |
4 | |
8 | |
8 |
The above is the detailed content of A brief analysis of Java object size. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
