Object size
It is mainly composed of 3 parts, including object header, instance variables and memory filling. as the picture shows:
note:
1. The object header is related to the number of virtual machines. In a 32 -bit virtual machine, the object header is 8 bytes (including 4-byte Class pointer and 4-byte MarkWord. In a 64 -bit virtual machine, the object header is 16 bytes (including 8-byte Class pointer and 8-byte MarkWord. If 64-bit pointer compression is enabled, the object header storing the Class pointer is 12 bytes (because the Class pointer is compressed into 4 bytes).
2. Instance objects are defined in the class Various types of field content. For example:
3. Memory filling : used to ensure that the number of bytes (object header + instance variable) must be a multiple of 8.
Basic data type length
View the size of the object opened up on the heap:
Idea: The
mian function is set to an infinite loop, and the corresponding entity can be found before the program ends. Generate the log file, you can view the occupied space.
Steps:
1. Use Notepad to create two new java files and put them in the same path.
TestDemo.java
public class TestDemo{
public static void main(String[] args){
People p=new People();//注意P大写
while (true){
}
}
}
People.java
class People{
int a;
int b;
int c;
byte d;
}
2. In the java file directory of the search box enter cmd, open two spare DOS interface.
3. In the first cmd: input javac TestDemo.java
and in turn java TestDemo
to compile and run the java file.
In the second cmd, enter jps
(used to view the TestDemo process number) and jmap -histo:live 进程号>inform.log
(generate log file) in turn
4. Then check the inform file just generated in the directory where the java file is located.
5. Search for the keyword People to see the amount of space opened up by the virtual machine for this category.
6. Note that TestDemo.java is an endless loop. After getting the logn file, enter ctrl+c in the dos interface to terminate the program.
Size analysis:
Due to pointer compression, the object header is 12 bytes; there are 3 int and 1 byte variables in the People class, a total of 4*3+1= 13 bytes. 12+13=25 is not a multiple of 8, the system fills its memory with 7 , so the total size is 32.
In fact, the Class file in the memory is stored in the form of a byte stream. All 16bit, 32bit and 64bit data are constructed and stored by reading the memory space of 2bytes, 4bytes and 8bytes respectively. Multi-byte data items are always stored in high order, with high byte first. The largest data types long and double in Java both occupies 8 bytes , so the memory is allocated in units of the largest 8 bytes.
Benefits of memory alignment
1. Platform reason (transplant reason): Not all hardware platforms can access any data at any address. Some hardware platforms can only fetch certain types of data at certain addresses, otherwise hardware exceptions will be thrown
2. Hardware reason: After memory alignment, the design of the processor and memory interface is simplified, and the memory access speed of the CPU can be greatly improved. Suppose a processor wants to read 8 bytes of data A from the memory, the address of A must be a multiple of 8, so that a memory operation can be used to read or write this A. Otherwise, we may need to perform two memory accesses, because A may be placed in two 8bytes memory blocks.
Memory alignment is actually the idea of space for time. After all, the CPU is orders of magnitude faster than the memory.
Reference materials:
Why does The Java® Virtual Machine Specification need to be memory aligned?
Memory alignment rules
object memory layout
CPU and memory speed comparison