Discussion on garbage collection (GC) in C ++)

Source: Internet
Author: User
Tags microsoft c
This article has been migrated to: http://cpp.winxgui.com/cn:garbage-collection-in-cplusplus

Use smart pointers)
  • Http://www.codeproject.com/cpp/garbage_collect.asp
  • Principle: reference count ).

    This garbage collection (GC) algorithm provides reference counting for each allocated memory, and then automatically adds or removes the reference count through the smart pointer. If the reference technology is reduced to zero, this means that no one uses the memory, and the memory can be recycled.

    If you can correctly follow the GC usage rules, that is, you can use smart pointers correctly, there is no possibility of Memory leakage theoretically.
    But the reality is not as simple and beautiful as you think. On the one hand, the concept of smart pointers is still not widely understood and applied by the C ++ community, and there are a wide variety of smart pointers, each with its own rules. When the amount of code is too large, sometimes such mistakes are inevitable. On the other hand (it should be even more difficult), not everyone is so fond of Using Smart pointers, in addition, native pointers can be avoided in many cases. We have to convert native pointers to smart pointers.
    Therefore, the final result of GC is that it seems that it is not much easier to master the reference counting rules than to master the new/delete rules, and sometimes it may even make new programmers afraid of the technology. In addition, the introduction of reference counting has unexpectedly brought about a negative effect: Debugging Memory leakage has become much more difficult than previously.
    Therefore, GC implemented by reference counting and smart pointers cannot fundamentally solve the memory leakage problem.

Real garbage collection)
  • Note that C ++ does not support GC. We know that bn always emphasizes that GC is an optional component of C ++ and is not necessary.
  • Here is the c ++ GC Code and documentation:
    • C ++/CLI (managed by Microsoft C ++)
    • Http://www.hpl.hp.com/personal/Hans_Boehm/gc/ (many people use this GC for development)
    • Http://www.devarticles.com/c/a/Cplusplus/A-Simple-Garbage-Collector-for-C-plus-plus/
    • Http://cybertiggyr.com/gene/garbage-collection-cpp/
  • Comment: If your system is simple, it is a good idea to use it. This means that your c ++ language is no different from Java and C. However, please note that this requires your system to be pure, that is:
     
    • You are not using third-party code. Or, in your third-party code, the memory is also managed (created by GC ).
    • Be careful with the DLL (for example, Win32 API/third-party components you purchased) that you cannot obtain the source code. Like C #, you need to be careful not to expose your program to a situation where Win32 APIs are still accessing data and GC recycles the data. However, this situation is much better than C # In C ++: after all, in C ++, your GC control capability is far stronger than C #.
    • In particular, if you use Com, you will suffer: Because com manages object lifecycle based on reference count, which means GC is basically powerless. Unless you provide a wrapper for each COM component like C.

Supplement:

  • GC depends on the reflection ability of the language, and C ++ is undoubtedly quite weak in this aspect, so GC in C ++ is embarrassing.
  • Note that GC is exclusive, which means that the two GC cannot work together. Therefore, unless the C ++ Standard specifies that the GC must be used, it is difficult to communicate with the hosted C ++ code.
Allocator (GC Allocator) that supports garbage collection)
  • Code and documentation:

    • C ++ memory management revolution
    • Autofreealloc (Pocket Garbage Collector)
    • Boost: object_pool (note,Boost: pool does not belong to GC AllocatorBut can only be said to belong to Allocator)
    • To be continued...
  • Rating: semi-automatic garbage collection is simpler, more efficient, and easier to control than GC.
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.