--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA%3DXfu3xrghcLhrj3La1niVnHs07DdCUUDfWjpVTyTUkhMwTjA%40mail.gmail.com.
-----Original Message-----
From: Tyler Compton
Sent: Feb 14, 2020 2:57 PM
To: Robert Engels
Cc: golang-nuts
Subject: Re: [go-nuts] What are finalizers missing?Thanks for the pointer! I took a look at PhantomReferences and Cleaners, and I see how they could be a more robust way to free resources for an object. However, I don't see a mechanism by which they can express to the garbage collector how much "pressure" they exert on a finite resource (file descriptors, memory not managed by the Java GC, etc). I don't know very much about the Java GC, but in a scenario where we have a few Java objects with a low amount of memory usage but with a large number of (for example) file descriptors, the Java GC might just decide not to run because it has no knowledge of the fact that running would help free up file descriptors.In other words, I see how PhantomReferences and Cleaners are less brittle than runtime.SetFinalizer, but not how they're more capable of managing resources that the GC is not already aware of.
On Fri, Feb 14, 2020 at 11:17 AM Robert Engels <ren...@ix.netcom.com> wrote:
I suggest you look at PhantomReference in Java, and the Cleaner/Disposer classes/patterns for how this would work. Go doesn't have PhantomReferences, so it's moot at this time.
-----Original Message-----
From: Tyler Compton
Sent: Feb 14, 2020 1:09 PM
To: golang-nuts
Subject: [go-nuts] What are finalizers missing?
The conventional wisdom I see in the Go community is to avoid using finalizers to manage file descriptors, C memory, or any other non-Go memory resource. The generally accepted rationale (at least to my understanding) is that the garbage collector cannot be expected to run the finalizer promptly, and it may never be run at all.This is where I start speculating. I assume this is because the garbage collector has no understanding of C memory, file descriptors, or any other kind of resource. If we're running out of memory due to a malloc in C, there's no way for the garbage collector to know which Go objects it can finalize in order to get that memory back. However, as a thought experiment, if we were able to tell the garbage collector (say, through a function in the runtime package) that a certain Go object owns X number of file descriptors or X bytes of C memory, could the garbage collector be augmented to free these objects (and thus run their finalizers) intelligently? Would doing so allow us to use finalizers for these resources with confidence?I don't mean this to be a proposal or suggestion. I'm asking more as an opportunity to learn.
--
On Feb 14, 2020, at 6:24 PM, robert engels <ren...@ix.netcom.com> wrote:
The conventional wisdom I see in the Go community is to avoid using finalizers to manage file descriptors, C memory, or any other non-Go memory resource. The generally accepted rationale (at least to my understanding) is that the garbage collector cannot be expected to run the finalizer promptly, and it may never be run at all.
This is where I start speculating. I assume this is because the garbage collector has no understanding of C memory, file descriptors, or any other kind of resource. If we're running out of memory due to a malloc in C, there's no way for the garbage collector to know which Go objects it can finalize in order to get that memory back.
So it's not a matter of the go runtime not knowing "which Go objects it can finalize in order to get that [a resource] back", its simply a matter of the variable pace of GC. In theory GC can happen as infrequently as every 2 minutes. So if you are ok with you non-Go resource lingering for that long, then use finalizers. In most cases that is not sufficient though.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9d2fd31b-f283-4dec-bb38-8cc8cc844249%40googlegroups.com.
-----Original Message-----
From: Tyler Compton
Sent: Feb 18, 2020 11:29 AM
To: Jake Montgomery
Cc: golang-nuts
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA%3DXfu1OF_tBMKXgJKCFU9U_SNvdiKTvg1c%2B9-4KFL7e5jo5bA%40mail.gmail.com.