In-depth understanding of the Java virtual machine (notes)

Memory allocation:

  There are two ways to allocate memory for objects. The first is "pointer collision", that is, the memory is divided into two sides, one is the used area, and the other is the unallocated area. The dividing line is recorded with pointers. When memory is to be allocated , just move the pointer to the unallocated area to the required space, usually the garbage collection of the compact algorithm will use "pointer collision", such as Serial, ParNew; the other is the free list record, that is, the allocation can be discontinuous , there are many unallocated memory available at intervals in the middle. At this time, a list is needed to record the memory. When allocating memory, the most suitable one is found in the list. Usually, the garbage collector corresponding to this allocation method, such as CMS, is based on Mark-Sweep algorithm;

  Since allocating memory is also multi-threaded, there is competition for memory usage resources. Therefore, to ensure thread safety, there are two solutions to this problem. The first is to use CAS plus the failure retry method to maintain atomicity; the second is to use The idea of ​​ThreadLocal is to allocate a thread's own space for each thread, called Thread Local Allocation Buffer, TLAB. When the thread completes the escape analysis, the object is allocated to this area. Finally, when the thread is allocated, the virtual machine immediately initializes the allocated space to a zero value, excluding the object header.

Object header (Header):

  Objects can be divided into three parts, object header, instance data, and alignment padding;

  The object header includes two kinds of information, the first is the runtime data (Mark Word), and the second is the type pointer (used to specify which class the object is created by, but it is not necessarily implemented like this); the runtime data includes: Ha Hope, GC generation age, lock status flags, locks held by threads, biased lock IDs, biased timestamps, etc. If the object is an array, then the object header will also continue the array-sized data.

  Then there is the real instance data. The instance data includes those inherited from the parent class and those owned by the child class. The fields of the parent class will be prioritized in the first order, and the same size will be allocated together according to the size of the basic type. The final data alignment is simply padding.

 

,,,,,to be continued

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324624072&siteId=291194637