You know those things JVM memory of it?

Foreword

For programmers C language development, the memory management must be responsible for the life cycle of each object, and from there to.
For Java programmers to you, with the help of virtual machine memory management, does not need to match free operation, memory leaks and memory overflow issues for each of the new objects are also less likely to occur, but it is precisely because the Memory Management to the virtual machine, run the program once there was a memory leak, causing great difficulties to the investigation process. So only understand the operating mechanism of the Java virtual machine to be able to make plans for a variety of code. In this paper, an example of those things to talk about HotSpot virtual machine.

JAVA virtual machine to manage memory is divided into several different data areas.


webp

Java heap

Java heap is shared by all threads of a memory area for storing main object instance, Java Virtual Machine Specification such a description: All examples and data objects to be allocated on the heap. Allocate memory for the object is to determine the size of a division of memory from the heap memory, there are usually two methods:

1, the pointer collision method
assumptions Java heap memory is full, the allocated and free memory are at different side, through a dividing point as a pointer, the memory needs to be allocated, only needs to move the pointer to the free end of a equal size of the object distance.

2, the free list method
fact, Java heap memory is not full, the allocated and free memory interleaved with each other, through the JVM maintains a list of memory blocks recorded information is available, when the dispensing operation occurs, found from the list a large enough block of memory allocated to the object instance, and recorded in the update list.

Object creation is a very frequent behavior, heap memory allocation Shihai multithreading issues to consider, is not yet possible to update the object A memory allocation, pointer, or record, while the object B and assigned to the original memory to solve this problems there are two options:
1, using CAS guarantee the atomicity of data updating operation;
2, the behavior of the memory allocation is divided according to the thread, performed in different spaces, each thread stack pre-allocate a block of memory in the Java, called a local buffer allocated thread (thread local allocation buffer, TLAB) ;

Java stack

Java thread stack is private, a Java stack for each thread, each thread will create a corresponding stack frame (Stack Frame) in the implementation of a method stack frame variable table is responsible for storing local variables, operand stack, dynamic link methods and return address and other information. Each method invocation, the equivalent of the Java stack frame stack pushing and popping process.

webp

Local variable table for storing method parameters and local variables defined within the method, the size has been determined during the code compilation, the method does not change during operation. Local variable table to a variable slot (Slot) is the smallest unit of storage, each capable of storing a Slot boolean, byte, char, shot, int, float, reference returnAddress type and 32-bit data, 64-bit data types for long and double virtual opportunity to align the high way to assign two consecutive Slot space.

When performing the method, if the method is an instance, i.e., non-static method, the local variable table Slot 0 of the object instance stored reference default and can be accessed by a keyword in this method, the process parameters according to the parameter list order, from Slot allocation starts a process for the rest of the internal variables Slot allocation order of definition.

class test {
    public int calc(int a, int b, String operation) {
        operation = "+";        return  a + b;
    }    public void main(String args[]) {
        calc(100, 200, "+");
    }
}

对应的局部变量表如下:


webp


使用 javap -c 命令查看方法calc的字节码


webp

其中iload_1和iload_2分别从局部变量表中的第1位和第2位中加载数据。

方法区

方法区和Java堆一样,是所有线程共享的内存区域,用于存放已被虚拟机加载的类信息、常量、静态变量和即时编译器编译后的代码等数据。
运行时常量池是方法区的一部分,用于存放编译期间生成的各种字面常量和符号引用。

指令计数器

指令计数器是线程私有的,每个线程都有独立的指令计数器,计数器记录着虚拟机正在执行的字节码指令的地址,分支、循环、跳转、异常处理和线程恢复等操作都依赖这个计数器完成。如果线程执行的是native方法,这个计数器则为空。

对象的内存布局

对象在内存中布局可以分成三块区域:对象头、实例数据和对齐填充。
1、对象头
对象头包括两部分信息:运行时数据和类型指针,如果对象是一个数组,还需要一块用于记录数组长度的数据。

1.1, the runtime data comprises a hash code (HashCode), GC generational age lock state flag thread holds the lock, and biased toward the lock ID timestamp, this part of the data in 32-bit and 64-bit virtual machine lengths of 32bit and 64bit, officially known as "Mark Word". Mark Word is designed to be non-fixed data structure, in order to achieve as much data stored in a limited space.
32-bit virtual machine, the state of the object is not locked, the 25bit HashCode Mark Word store objects of 32bit, 4bit storage object generational Age, 2bit lock flag storage address, 1bit fixed at 0, as follows:


webp


The stored content (lock lightweight, heavyweight lock, locking the GC may be biased locking) Mark Word under the other states as follows:


webp


1.2, the type of the object pointer points to the first class of the object metadata, the virtual machine by this pointer can be determined which instance of the object class.

2, examples of the data
instance field types of data is defined in the program code, including inherited from the parent class, this part of the storage order is affected by the virtual machine in the field and the allocation policy defined in the source sequence.
3, alignment padding
since HotSpot automatic memory management requires the starting address of the object must be an integer multiple of 8 bytes, i.e., the size of the object must be an integer multiple of 8 bytes, data object header exact multiple of 8, so that when the instance data byte not an integer multiple of 8, it needs to be filled by the alignment completion.



Guess you like

Origin blog.51cto.com/14028890/2409623