Method used in a third-party garbage collector

I am writing to clarify some comments on this website.

1) I know that C ++ does not have a garbage collector. One said that C ++ was invented before the idea of โ€‹โ€‹a garbage collector, so thatโ€™s the reason. It's true? I think that makes sense.

2) Whenever the garbage collector was discussed, a smart point was selected (e.g. boost :: share_ptr). I once made sure that reference counting is one way to implement a garbage collector, but some say that a smart point is not an implementation of a garbage collector. What happened?

3) Some said why the garbage collector was not included in C ++ because it was difficult and many problems could not be solved. However, someone else said that there is a third-party garbage collector, regardless of whether it is commercial or free. So how do these third parties handle problems?

I am grateful if anyone can clarify my perplexities.

Many thanks!

+4
source share
5 answers
  • no, garbage collection is much older than C ++ (many Lisp versions were in the 60s, in particular).

  • link counting is a way to implement garbage collection, but it is rather poor performance wise (the new empty Swallow project, speed up the CPython interpreter, includes a switch from link counting to more efficient garbage collection - this is a significant increase in performance).

  • The Boehm collector for C and C ++ uses a conservative approach: in short, everything that looks like an address is taken to be one (so no matter what it says "not going to"). Read the page at the specified URL and its outbound links for more information on this.

+4
source
  • As Alex noted, LISP has had garbage collection ever since C ++ was invented. OTOH, some of these early implementations used reference counting.

  • Most people discussing garbage collection think of something hidden. The link counter has a number of problems. It may have performance issues, but since objects are commonly used in C ++, it rarely does. A much bigger problem is that reference counting is usually not related to loops in data. For a trivial example, consider:

    struct node {node * next; };

    node * node1 = new node, node2 = new node;

    node1-> next = node2; node2-> next = node1;

    Since each node now references a different one, their reference counts will remain non-zero, even if none of the others refers to one of them. They are not going, even after they become inaccessible. However, this problem is surmountable.

  • When you use third-party garbage collectors with C ++, the result is not a (fully) compatible implementation of C ++. For example, if you "swizzle" a pointer (for example, invert all its bits), the GC will not know what it points to. When you "do not swizzle" him (lay back), then what he pointed out can no longer exist. However, problems with real code are quite unusual that people regularly use GC without any problems.

    OTOH, there are also limitations, so most garbage collectors for C and C ++ do not work very well either. The latest GC developments work by copying objects that still exist, so they are all contiguous on the heap after the GC cycle has been run. To do this, it must "fix" all pointers to this object in order to point to the new address. Since a GC with C or C ++ does not know exactly which pointer or not, it cannot change things (in case something is not a pointer), so it must leave objects in place, which degrades performance.

There were serious discussions about adding GC to C ++. Two comp.lang.C ++. moderated threads can be interesting. Warning: they are quite long, and some arguments are repeated several times in several cases. OTOH, they point to some real problems and possible solutions.

+3
source

As for 1 and to a lesser extent 2:

This is a short but good explanation.

http://www.devx.com/tips/Tip/12766

Plus, here is a similar question posted here about the topic.

Why doesn't C ++ have a garbage collector?

Check them out.

+1
source

According to http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 Garbage collection was invented in 1959, which would significantly change the C ++ date.

The reason C ++ doesn't include the garbage collector is because you, the programmer, manage the memory, a function inherited from C. The garbage collector is part of the language that the memory management is done for you. You can implement it, but you will have to use your implementation to control all access to memory - otherwise you would get along. In short, GC is part of the layer between you (in any language you are in) and the systemโ€™s core memory.

0
source

Minimal support for garbage collection, but not a garbage collector, will be added to the next C ++ standard (informally called C ++ 0x). Here is a good article about it: http://www.hpl.hp.com/techreports/2009/HPL-2009-360.pdf

0
source

Source: https://habr.com/ru/post/1302198/


All Articles