Java garbage Collection related issues

Source: Internet
Author: User

An overview of the various parts of the Java Memory Runtime area, where the program counters, virtual machine stacks, the local method stack 3 regions with the thread, and out of the thread, stack frames in the stack with the method entered and exited and methodically carried out the stack and into the stack operation. Second, the object of death judgment 1. Reference counting Algorithm(1) Decision algorithm: Add a reference counter to the object, each time a place reference it, the counter value is added 1, when the reference fails, the counter value is reduced by 1, any time the counter is 0 of the object can no longer be used.
(2) Disadvantage A, it is difficult to solve the problem of mutual circular reference between objects. As follows:
  1. class TestA{
  2. public TestB b;
  3. }
  4. class TestB{
  5. public TestA a;
  6. }
  7. public class Main{
  8. public static void main(String[] args){
  9. A a = new A();
  10. B b = new B();
  11. a.b=b;
  12. b.a=a;
  13. a = null;
  14. b = null;
  15. }
  16. }
Although, yes, A and B are null, but because of circular references, all are never recycled. b, a waste of CPU, time counter count statistics, even if the memory is sufficient. 2. Accessibility Analysis Algorithm(1) algorithm: Through a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, when an object to the GC Roots no reference connected, it proves that this object is not available.

Note: Objects Object5, OBJECT6, object7, though interconnected, are not accessible to GC roots, so they are judged as recyclable objects.
Third, garbage collection algorithm 1. Mark-Sweep algorithm(1) Algorithm idea: First mark out all the objects that need to be recycled, after the completion of the mark to collect all the unmarked objects, if the object after the accessibility analysis found that the GC roots is unreachable, then it will be the first time to be marked and filtered, The filter condition is whether this object is necessary to execute the Finalize () method. When an object does not overwrite this method, or has been called by a virtual machine, both cases are considered unnecessary by the virtual machine. If necessary, the object will be placed in the F-queue queue, and later the GC will make a second small-scale mark on the objects in F-queue, if the object succeeds in Finalize () to associate with any of the objects that are in the GC roots. If the association has not been established, it will be recycled. Icon:

2. Copy Algorithm(1) Algorithm idea: Divide the available memory blocks by capacity into two blocks of equal size, one at a time.                                When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once. Note: Divide the memory into a larger Eden space and two smaller survivor spaces, each using Eden and one of the survivor. When recycled, the objects that are still alive in Eden and survivor are copied to another survivor space at once, and finally the Eden and the newly used survivor space are cleared.                              The hotspot virtual machine default Eden and Survivor size ratio is 8:1. (2) Advantages: Simple implementation, efficient operation. Cons: Shrinking the memory to half the original price is too high. Icon:

3. Labeling and Sorting algorithm(1) Algorithm idea: The tagging process is still the same as the "tag-purge" algorithm, but the next step is not to clean up the recyclable objects directly, but to let all surviving objects move toward one end, and then directly clean out the memory outside the end boundary. Icon:

4, the generation of algorithms(1) Algorithm idea: Divide the memory into several blocks according to the different life cycle of the object.  Java is generally divided into the new generation and the old age, so that the characteristics of each age can be used to the most appropriate collection algorithm. Reference "1", "in-depth understanding of Java Virtual Machine" mechanical industry press Zhou Zhiming
                             

Java garbage Collection related issues

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.