-
|
I have a idea to optimize GC for class with finalizer: add automatical reference counter for such class and add the class object to finalization queue as soon as its reference count becomes zero. Then the object might be reclaimed by next GC, instead of "after several times of GC". |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Covered in #4029 (and linked issues). |
Beta Was this translation helpful? Give feedback.
-
|
It can hardly be done. GC and RC are fundamentally conflicting systems. Under GC, setting a reference variable is passive and atomic. It only involves one memory address. All of the existing code are relying on this behaviour. Finalizers aren't expected to be common in high-performance scenarios. The are expected to be canceled through |
Beta Was this translation helpful? Give feedback.
-
|
@ygc369 Finalizer is just a managed method. Placing in in the finalizer queue instead of simply calling the "Dispose" method directly will sinply result in worse performance. Also, "this way object can be collected" in the next gc would actually be true for current gc because having ReferenceCount of 0 is basically similar to having no incoming references (unreachable). GC may collect it or not and it's up to the backing algorithm. I think ultimately only a lifetime/ownership system can solve such issues. With such you could just write your own RC trackers or other things and be sure "Dispose" is called automatically when object goes out of scope. |
Beta Was this translation helpful? Give feedback.
@ygc369 Finalizer is just a managed method. Placing in in the finalizer queue instead of simply calling the "Dispose" method directly will sinply result in worse performance.
Also, "this way object can be collected" in the next gc would actually be true for current gc because having ReferenceCount of 0 is basically similar to having no incoming references (unreachable). GC may collect it or not and it's up to the backing algorithm.
I think ultimately only a lifetime/ownership system can solve such issues. With such you could just write your own RC trackers or other things and be sure "Dispose" is called automatically when object goes out of scope.