runtime.SetFinalizer
. Let's look at it in work: package main import ( "fmt" "runtime" "time" ) type Test struct { A int } func test() { // a := &Test{} // runtime.SetFinalizer(a, func(a *Test) { fmt.Println("I AM DEAD") }) } func main() { test() // runtime.GC() // time.Sleep(1 * time.Millisecond) }
I AM DEAD
a
, which is a pointer, and set a simple finalizer on it. When the test()
function completes, all references to a
disappear, and the garbage collector is given permission to assemble it and, therefore, call the finalizer in its own gorutin. Try changing the test()
so that it returns *Test
and prints it in main () - you will find that the finalizer was not called. The same thing happens if you remove the A
field from the Test
type - the structure will be empty, and the empty structures do not take up memory and do not require cleaning by the garbage collector. runtime.SetFinalizer(fd, (*netFD).Close)
Close
on net.Conn
.Close
rather than use the magic of finalizers. For example, here is the os finalizer that handles the file descriptor: func NewFile(fd uintptr, name string) *File { fdi := int(fd) if fdi < 0 { return nil } f := &File{&file{fd: fdi, name: name}} runtime.SetFinalizer(f.file, (*file).close) return f }
os.NewFile
is called by the os.OpenFile
function, which in turn is called from os.Open
, so this code is executed each time the file is opened. One of the problems of the finalizers is that they are beyond our control, but, worse, they are unexpected. Take a look at the code: func getFd(path string) (int, error) { f, err := os.Open(path) if err != nil { return -1, err } return f.Fd(), nil }
getFd
the f
object loses the last link, and your file is doomed to close soon (at the next garbage collection cycle). But the problem here is not that the file is closed, but that this behavior is undocumented and completely unexpected.os.Open
), should have a corresponding mention in the documentation. I personally find this method useless and may even be a bit harmful.Fd
method is now documented: link . That once again confirms that it would be nice to document your finalizers too.Source: https://habr.com/ru/post/268841/
All Articles