For the garbage collection mechanism of the JVM, the standard of whether to reclaim an object is whether there is a reference variable referencing the object, and the garbage collection mechanism does not recycle it as long as it is referenced by a reference variable.
Strong reference: Creates an object and assigns the object to a reference variable. This reference is not recycled by the garbage collection mechanism, and the system resources are very tight in time, even though some Java objects will never be used, and the JVM will not reclaim Java objects referenced by strong references.
Soft reference: It needs to be implemented by the SoftReference class, which can be reclaimed by the garbage collection mechanism when an object has only soft references. When the system memory space is sufficient, it will not be reclaimed by the system, and when the system memory space is insufficient, the system recycles it.
Weak references: Similar to soft references, except that reference objects have a shorter lifetime. Weak references are implemented through the WeakReference class. For objects that have only weak references, when the system garbage collection mechanism runs, the memory consumed by the object is reclaimed, regardless of whether the memory is sufficient.
Virtual reference: cannot be used alone. Role: The primary is to track the state of the object being garbage collected, and the program can understand whether the object referenced by the virtual reference is about to be reclaimed by checking that the reference queue associated with the virtual reference already contains the specified virtual reference.
Basic algorithms for garbage collection:
- Serial recycling (serial) and parallel recycling (parallel): Serial recycling is the process of using only one CPU to perform garbage collection operations, regardless of the number of CPUs in the system, and parallel recycling is to split the entire collection into multiple parts, one for each CPU, to allow multiple CPUs to be recycled in parallel, Parallel recovery is highly efficient, but increases in complexity, as well as the problem of increased memory fragmentation.
- Concurrent Execution (concurrent) and application Stop (Stop-the-world): Stop-the-world garbage collection can cause the application to pause while the garbage collection is being performed. While garbage collection that executes in parallel does not cause the application to pause, more memory is required because concurrent garbage collection needs to be addressed and application execution conflicts occur.
- Compression (compacting) and non-compression (non-compacting) and Replication (copying): In order to reduce memory fragmentation, a garbage collection that supports compression will move all live objects together and then reclaim all of the previously occupied memory. The uncompressed garbage collector simply reclaims the memory, so that the recovered memory cannot be contiguous, so there will be more memory fragmentation than the compressed garbage collection, the uncompressed garbage collection reclaims the memory blocks, and the memory allocation is slower, and the memory fragmentation problem cannot be resolved. A replicated garbage collection copies all the available objects into the same memory, which is a bit of a garbage collection process that does not produce memory fragmentation, but the disadvantage is that you need to replicate the data and additional memory.
Note: Tag clear: Non-compressed recycle mode
Tag compression: How compression is recycled
The above replication, non-compression, and compression are the way that the garbage collector reclaims the used memory space.
The current recycling mechanism is: depending on the length of time the object lives. Divide the memory into 3 generations:
- Young (Youth band)
- Old (older age)
- Permanent (permanent Generation)
Java in-store management