6 garbage-collection finalizer go
该函数runtime.SetFinalizer(x, f interface{})设置与xto 关联的终结器f.
默认情况下最终确定了哪种对象?
默认情况下最终确定这些对象会导致哪些意外陷阱?
小智 8
默认情况下,以下对象已完成:
操作系统.文件:当对象被垃圾收集时,文件自动关闭.
操作系统.流程:Finalization将释放与流程相关的所有资源.在Unix上,这是一个无操作.在Windows上,它会关闭与进程关联的句柄.
在Windows上,似乎程序包net可以自动关闭网络连接.
Go标准库没有在上面提到的对象类型上设置终结器.
似乎只有一个可能导致实际程序出现问题的潜在问题:当a os.File完成时,它将调用OS来关闭文件描述符.如果os.File已通过调用函数创建os.NewFile(fd int, name string) *File并且文件描述符也被另一个(不同)使用os.File,则垃圾收集其中一个文件对象将使另一个文件对象不可用.例如:
package main
import (
"fmt"
"os"
"runtime"
)
func open() {
os.NewFile(1, "stdout")
}
func main() {
open()
// Force finalization of unreachable objects
_ = make([]byte, 1e7)
runtime.GC()
_, err := fmt.Println("some text") // Print something via os.Stdout
if err != nil {
fmt.Fprintln(os.Stderr, "could not print the text")
}
}
Run Code Online (Sandbox Code Playgroud)
打印:
could not print the text
Run Code Online (Sandbox Code Playgroud)
只需跳转到os.NewFile的源代码:
// NewFile returns a new File with the given file descriptor and name.
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
}
Run Code Online (Sandbox Code Playgroud)