What examples of algorithms and / or data structures are difficult or impossible to implement correctly without garbage collection?

I heard that in general terms it was mentioned that such things exist, but the details are rarely discussed. What are your favorites? What is in the way?

+3
source share
4 answers

The standard implementation of many loose constructions, such as a parallel hash table, is often nearly impossible to write without a garbage collector. These structures work by storing long linked lists of items and then changing the heads of the lists whenever a new value is added or removed. Thus, one thread can make changes to the structure so that new threads consider the change (they cross the new linked list), while old threads continue reading the old linked lists. It is imperative that the memory be corrected by the garbage collector, because otherwise the thread that changed the linked list must somehow clear the list that it just replaced, but this list is used by other threads, it either leads to data races , or requires the use of locks,both of which are bad.

, .

+4

GC, GC .:-) GC , GC. GC , -GC-.

, /, (.. , ) - . , - php asp.net, php/# . - -, script, - ...

, , . - .

LINQ # . .

+3

, . , ( / ). : [ " ABA" - ] (ABA - , , ) GC? , . . GC. , : http://www.1024cores.net/home/lock-free-algorithms/object-life-time-management/differential-reference-counting Another solution is to develop an algorithm around the requirement, so it just does not require a GC. For example, for some unprotected consumer-producer chains (most visible for the M & S queue), GC is required. And here is a simple efficient queue algorithm that is specifically designed around the GC requirements: http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue

+3
source
var v = FunctionThatAllocatesMemory2(FunctionThatAllocatesMemory1());

Without a GC, there is no way to free the memory returned FunctionThatAllocatesMemory1().

0
source

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


All Articles