○ Supplementary – Array length

Complement array length:

1. If not compressed, it is allocated after the non-static fields declared in arrayOopDesc.

The klass pointer + array length is 12 bytes eg: 11111 11111

2. If compressed, it will occupy the bottom half of the _klass field in oopDesc

It takes 8 bytes to store the klass pointer + array. Because the compressed Klass pointer takes up only four bytes of itself, there are four empty bytes left to store the length of the array, rather than applying another eight bytes of length and then squeezing it down to four bytes. Eg: 1111 [extra four word save array length: 1010]

How did I get 4 bytes out of this? Since the storage type and array length are combined, the union size is 8 bytes. If compression is turned on and only 4 bytes are used, the remaining 4 bytes are wasted, so the array length is used. Because the array length is exactly int, it takes up four bytes.

Let’s look at a union that stores array lengths:

union_metadata { Klass* _klass; // narrowKlass = narrowKlass; 4B} _metadata;Copy the code

The entire union takes 8 bytes

  • The JVM has four modules: 1, class loading 2, memory model 3, execution engine 4, garbage collector
  • Among them, garbage collector is based on garbage collection algorithm implementation.
  • Garbage detection algorithm
  • Garbage collection algorithm

Mark clear algorithm: Mark live objects or objects waiting to be reclaimed?

I sorted out some information, and friends in need can click to get it directly.

Java Basics collection

One hundred Core Java architect books

Standard Ali P7 Java architect learning roadmap

The latest interview questions in 2021

Garbage detection algorithm

Reference counting method

Similar to a reentrant lock, this number is +1 after the lock is entered and -1 after the lock is released.

Add an attribute to an object to mark the number of times the object has been referenced. For each additional object reference, count +1. When the reference is invalid, count -1. This algorithm cannot solve the problem of cyclic dependency.

Accessibility analysis

Through a series of root objects called “GC Roots” as the starting node set, from these nodes, search down according to the reference relationship chain. If an object cannot be found, it means that the object has no reference execution and can be recycled. Conversely, the object is alive and not recyclable. The implementation in the JVM is to find live objects, and unmarked objects are useless and will be collected in GC.

Which objects can be used as GC Root: • References to objects in the GC heap in the currently active stack frames of all Java threads; In other words, the parameters/local variables/temporary values of the reference type of all methods currently being called. • References to objects in the GC heap in some static data structures of the VM, for example Universe in HotSpot VM has many such references. • JNI handles, Includes global handles and Local Handles • (depending) all currently loaded Java classes • (depending) Java Class reference type static variables • (depending) Java Class reference type constants (String or Class type) in the Runtime constant pool of a Java Class • String A reference to a constant pool

Copy algorithm: mark the surviving object and recycle the unmarked object;

Memory allocation algorithm

When a virtual machine arrives at a new instruction, it first checks to see if the instruction’s arguments locate a symbolic reference to a class in the constant pool, and to see if the class represented by the symbolic reference has been loaded, parsed, and initialized. If not, the corresponding class loading process must be performed first.

After the class load check passes, the virtual machine next allocates memory for the new objects. The size of memory required by an object is fully determined after the class is loaded, and the task of allocating space for an object is equivalent to dividing a certain size of memory from the Java heap. If memory in the Java heap is perfectly neat, all used memory is placed on one side, free memory is placed on the other, and a pointer is placed in the middle as a pointer to the dividing point. The allocated memory simply moves that pointer to the free space by an equal distance to the size of the object. This allocation is called a “Bump thePointer.” If memory in the Java heap is not neat, used memory and free memory cross each other, that is simply no way pointer collision, the virtual machine, you must maintain a list of records on which memory blocks are available, at the time of distribution from the list to find a large enough space division to the object instance, and update the list of records, This type of allocation is called a “FreeList.” The choice of allocation method depends on whether the Java heap is clean, which in turn depends on whether the garbage collector used has collation capabilities. Therefore, when collectors with Compact procedures such as Serial and ParNew are used, the allocation algorithm is pointer collision, whereas when collectors with mark-sweep algorithms such as CMS are used, the free list is usually used.

Pointer collisions (based on this implementation in the Open JDK, unlimited CAS, spinlock)

Take the address of the pointer and add the size of the object. Then determine whether the sum is less than the total memory size. Since memory allocation is concurrent, CAS is used for comparison, and if it fails, goto Retry and allocation continues. If the CAS fails, it means that the block of memory has been used.

The free list

0 to 10 0-5: m_AVAILable_table Available memory. 6-10: m_IDLE_TABLE Idle list list<Memorycell> m_available_table. List <Memorycell> m_used_table; List <Memorycell> m_IDLE_table; // list<Memorycell> m_transer_table; // Memory to be swappedCopy the code

Memory pool and related algorithms

Figure out the characters first

  • OS heap
  • Memory Pool
  • Memory Chunk
  • Memory Cell

Memory Poo L Memory pool

An administrator manages memory blocks and does not directly hold memory

The memory pool maintains a block of memory using a list:

You can do the following: Apply for memory from the OS: malloc and calloc Release memory: There is no garbage collector and you need to release memory manually. Other: Print chunk information

Memory Chunk Memory Chunk

Hold memory management blocks directly

The allocated Memory is divided into 8 bytes for each Memory Cell

Memory Cell

The actual memory in use the size of bytes per cell is determined by the developer. In the JVM it is 8 bytes

Analysis of each role

OS heap: The operating system is also a heap with a memory model (OS heap) stack static area code area readable, writable and executable

Memory Pool

MemoryChunk *new_chunk(uint_size); 1. Calculate how much memory you want from the OS based on the memory you want and alignment (8B); If 79B is required, then 80B memory will be requested from the OS based on 8-byte alignment calculations. 3. According to different garbage collection algorithms, fill different list tags to clear and organize, and these two algorithms recycle the whole chunk. These algorithms require only two lists to play with list<Memorycell> m_available_table; List <Memorycell> m_used_table; // All used memory generation + copy algorithm: 50/50, half in use, generally free. List <Memorycell> => list<Memorycell> => list<Memorycell> m_available_table; // All available memory 0-10 cell list<Memorycell> m_used_table; List <Memorycell> m_IDLE_table; M_transer_table <Memorycell> m_transer_table; // void print_chunks(); // void free_chunks(); // Free the memory. If you want to use C or C++, you need to free the memory manuallyCopy the code

How do you write the memory algorithm, depending on which garbage collection algorithm you want to support

Garbage collection algorithm

1, the memory 55 points;

Mark-clear algorithm

Iterate through all GC Roots, and then mark all objects reachable by GC Roots as living objects.

Facing the whole heap, fragmentation occurs

If you need to allocate large objects, you need contiguous memory and your memory is fragmented, you can’t allocate memory. So there is the tag collation algorithm, but the tag collation algorithm is CPU intensive.

As we know, objects in Eden area usually die out after enrollment. Objects are usually recycled in the first GC, which has a short life cycle and generates a large amount of memory fragments.

It is not that there is no memory, but that memory is not contiguous, causing the object to be unable to be allocated.

The cleanup process traverses all objects in the heap, removing all unmarked objects.

Mark-collation algorithm

The memory fragment merging algorithm in the old age is basically based on this algorithm, because the probability of retrieving objects in the old age is relatively low.

STW is required when you can defragment memory merge fragments

When merging fragments, there are two Spaces: 1, the objects in this space have been freed; 2, the objects in this space have not been freed (complex) : merging memory, moving data, and moving Pointers

Generation + copy algorithm

The purpose of existence: To solve the problem of mark-defragment algorithm, merge fragmentation consumption is too high, and gc stop user line is too long. 1. When gc occurs, switch roles:

Marked object cleanup Surviving objects need to be moved to a new memory area (11-20)

Conclusion:

Whether there are nine types of garbage collectors or new ones in the future, they are all based on these three collection algorithms.

What happens to the old pointer after the pointer moves?

After the move, the old address remains the same, very complicated. With the help of additional data structures, Pointers are computed dynamically. Here’s the formula:

Chunk ->get_data() obtains the first address of chunk,

conclusion

Memory pool JVM does not need us to manually release memory, garbage collector automatic collection; Garbage collection algorithm marking - Scavenging algorithm marking stage: traverses all GC Roots, and then marks all objects reachable by GC Roots as living objects scavenging stage: removes all unmarked objects. The two stages of mark-cleaning algorithm are basically the same as the mark-cleaning algorithm in memory consolidation: generation - copy algorithm because the performance of the consolidation algorithm is too low, and the new generation is characterized by the extinction of enrollmentCopy the code

The last

Give it a thumbs up for everyone who’s seen this