1. How do I determine if an object is a garbage?
Reference counting: simple, efficient, but how two objects are equal to null at the same time, they reference each other, resulting in their reference count not being 0 and never being recycled.
Accessibility Analysis Method:
2. Typical garbage collection algorithms
Tag-Clear algorithm: The most basic garbage collection algorithm, there is the marking phase and the purge phase; The task in the tagging phase is to mark all objects that need to be reclaimed, and the task of the purge phase is to reclaim the space occupied by the tagged object
Cons: Prone to memory fragmentation, too many fragments can cause subsequent processes to allocate space for large objects without finding enough space to trigger a new garbage collection action ahead of time
Copy algorithm: The available memory by capacity divided into two blocks of equal size, each time only use one of the pieces, when a piece of memory runs out, the remaining objects are copied to another piece, and then the used memory space once cleaned out, so that the memory is not prone to fragmentation.
Cons: Able to use half of the memory reduced to the original
Mark-and-organize algorithm: The tagging phase is like the tag cleanup algorithm, when the tag is finished, the surviving objects are moved to one end, and then the memory of the unexpected event is cleared out of the end boundary.
Generational collection algorithm: According to the life cycle of the object to divide the memory into several different regions, the heap zoning is divided into the old and the new generation, the characteristics of the old age is that only a small number of objects need to be recycled each time the garbage collection, and the characteristics of the new generation of garbage collected each time, there are a large number of objects to be , the old age uses the marking-sorting algorithm; There is also a permanent generation outside the heap, which is used to store class classes, constants, method descriptions, and so on, to reclaim two parts of the permanent generation: Obsolete constants and useless classes
3. Typical garbage collector
Garbage collection algorithm is the theoretical basis of memory recovery, and garbage collector is a concrete implementation of memory recycling.
Java garbage collection mechanism