Java garbage collection mechanism

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.