Is there a way to obtain the total number of finalizers registered using runtime.SetFinalizer
and which have not yet run?
We are considering adding a struct
with a registered finalizer to some of our products to release memory allocated using malloc
, and the object could potentially have a relatively high allocation rate. It would be nice if we could monitor the number of finalizers, to make sure that they do not pile up and trigger out-of-memory errors (like they tend to with other garbage collectors).
(I'm aware that explicit deallocation would avoid this problem, but we cannot change the existing code, which does not call a Close
function or something like that.)
You can keep keep a count of these objects by incrementing and decrementing a unexported package variable when a new object is created and finalized, respectively.
For example:
https://play.golang.org/p/n35QABBIcj