[CLR via C #] 21. Automatic Memory Management (garbage collection mechanism)

Source: Internet
Author: User
Tags finally block

Directory

  • Understand the basic working principles of the garbage collection platform

  • Garbage collection Algorithm
  • Garbage collection and debugging

  • Use the final operation to release local resources
  • End the use of managed resources

  • Why is the Finalize method called?

  • Close Operation secrets

  • Dispose mode: forces the object to clear Resources

  • Use the type that implements the Dispose Mode

  • C # using statement

  • Manual Monitoring and Control of object lifetime

  • Object resurrection

  • Generation

  • Thread hijacking

  • Large Object
1. Understand the basic working principles of the garbage collection platform
  • Hosting heap can do this because it makes a bold assumption that address space and storage are infinite. This assumption is obviously absurd. Therefore, the managed heap must use a mechanism to allow it to make such assumptions. This mechanism is garbage collection.
  • Objects are constantly created, and NextObjPtr is also increasing. If NextObjPtr exceeds the end Of the address space, it indicates that the managed heap is full, and a garbage collection is required.
  • Ii. Garbage collection Algorithm
  • Therefore, the garbage collector will cause significant losses, which is the main disadvantage of using managed heaps. Of course, garbage collection only happens when the 0th generation is full. Previously, the managed heap performance was much higher than that of the C Runtime heap.
  • Iii. Garbage collection and debugging 4. Use the final operation to release local resources
    Internal sealed class SomeType {~ SomeType () {// The code here will enter the Finalize method }}

    5. Compile the above Code and you will find that the C # compiler actually generates a protected override method named Finalize in the module metadata. The method subject is put into the try block, and the finally block is put into a call to base. Finalize.

    6. When the Finalize method is implemented, Win32 is generally called.CloseHandleFunction, and pass the local resource handle to the function.

    5. End the use of managed resources
  • We cannot control when the Finalize method will run. CLR does not guarantee the call sequence of each Finalize.
  • 6. What causes the Finalize method to be called? 7. Terminal operation secrets 8. Dispose mode: force the object to clean up resources. 9. Use the type that implements the Dispose mode.10. C # using statement 11. Manual Monitoring and Control of object survival 12. Object resurrection 13. Generation
  • Generation Principle:
  • 4. as before, after garbage collection, 0th generations of survivors were promoted to 1st generations, 1st generations of survivors were promoted to 2nd generations, and 0th generations were empty again, prepare for the arrival of new objects. Objects in the 2nd generation will undergo two or more checks. Objects in the 1st generation will be checked only when the 1st generation reaches the budget capacity. Previously, the 0th generation has been recycled several times.

    5. The managed stack of CLR only supports three generations: 0th generation, 1st generation, and 2nd generation. The budget for the 0th generation is about 1st kb, the budget for the 2nd generation is about 2 MB, and the budget for the generation is about 10 MB.

    14. Thread hijacking 15. large objects

    Related Article

    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.