Simulation of destructors in Clojure

Problem Statement

I have two cars: A and B, both running Clojure.

B has some memory data structure.

A contains an A_P object, which is a reference / pointer to some B_O object in memory B.

Now, while A_P is NOT GC-ed from A, I don't want B_O GC-ed by B.

However, as soon as A_P was GC-ed from A (and nothing else in B_O and nothing else in B refers to B_O), then I want B_O to be elegant as GC-ed.

Language solution with destructors

In C ++, this is easy - I use destructors. When A_P receives GC-ed, A sends B a msg to reduce the number of external links to B_O, and when it is 0 and internal links to B_0 are also 0, then B_O receives GC-ed.

Solution in Java / Clojure?

Now I know that Java has no destructors. However, I am wondering if the Clojure method has this problem.

Thanks!

+4
source share
4 answers

There is no good solution, without a real distributed garbage collector. Even in C ++, you cannot do it safely, because you did a reference count and pretended to be a real garbage collector; but if two objects point to each other by dividing the machine and both of them are not specified locally, they still have a non-zero reference counter and cannot be collected.

+3
source

No, Clojure (based on JVM, CLR) does not have "C ++ type destructors" due to the automatic JVM memory management model. There are things like finalizers, but it is recommended not to use them. Instead, you should model your solution based on the messaging engine, and not on a machine containing a "pointer / link" to the data in B. I know this answer is very high because you did not specify any specific details of the problem in your question. If you need more information on how to solve a specific problem, please provide the full context, and I'm sure someone can help you.

+1
source

This is an inherently difficult problem: the distributed garbage problem is really complex, if not impossible. [/ p>

However, you can simply make it work using Java finalists and override the finalize() method. Then you can implement a messaging method similar to the one you describe for C ++.

This will have problems in a more general case (it will not help you with circular links on machines, as indicated at the Amalie points), and there are some other quirks you need to know about (mainly around your lack of control over whether the finalist is called) but you can make it work in your specific situation.

+1
source

Assuming you are using a data structure such as ref or atom to store the data structure. Somewhere inside it, you can use listeners to monitor the state of this structure to delete A, and these listeners can send the corresponding message B. clojure.data/diff can be really useful for finding structures that have been deleted.

Another option is that immediately after the structure A is dereferenced, the function responsible for this will send a message. As part of this, however, make sure that this code was actually responsible for removing A, and not some other update.

0
source

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


All Articles