收集Go结构时是否有办法释放非托管资源?

问题描述:

我有一个指向由Go结构包装的C类型的指针,如下所示:

I have a pointer to a C type wrapped by a Go struct, like so:

type Wrapper struct {
    unmanaged *C.my_c_type
}

C类型又有以下功能:

The C type, in turn, has the following functions:

my_c_type* make_c_type();
void free_c_type(my_c_type *ct);

有没有一种方法可以确保 free_c_type $ c $每当一个 Wrapper 实例完成时调用c>

Is there a way that I can ensure that free_c_type is called whenever a Wrapper instance is finalized?

您可以使用 runtime.SetFinalizer 。这允许您在对象超出范围时运行清除功能。不保证运行。但是,释放记忆时,这并不重要。重要的是,对于长时间运行的流程来说,可能会对垃圾进行检查。

You can use runtime.SetFinalizer. This allows you to run a cleanup function when the object falls out of scope. It is not guaranteed to run. However, when freeing memory, that does not really matter. What does matter is that for a long running process, it is likely to keep the garbage in check.

以下是文档摘录(删除了整段内容):

Here are some excerpts from the docs (entire paragraphs were removed):


SetFinalizer将与x关联的终结器设置为f。当垃圾收集器发现具有关联终结器的不可达块时,它将清除关联并在单独的goroutine中运行f(x)。这使x再次可达,但现在没有关联的终结器。假设没有再次调用SetFinalizer,下次垃圾收集器发现x无法访问,它将释放x。

SetFinalizer sets the finalizer associated with x to f. When the garbage collector finds an unreachable block with an associated finalizer, it clears the association and runs f(x) in a separate goroutine. This makes x reachable again, but now without an associated finalizer. Assuming that SetFinalizer is not called again, the next time the garbage collector sees that x is unreachable, it will free x.

x的终结器计划运行在某些x无法到达后的任意时间。不能保证在程序退出之前终结器会运行,因此通常它们仅用于在长时间运行的程序期间释放与对象关联的非内存资源。例如,当程序放弃os.File而不调用Close时,os.File对象可以使用终结器来关闭相关的操作系统文件描述符,但依靠终结器来刷新内存中的I将是错误的/ O缓冲区,比如bufio.Writer,因为缓冲区在程序出口处不会被刷新。

The finalizer for x is scheduled to run at some arbitrary time after x becomes unreachable. There is no guarantee that finalizers will run before a program exits, so typically they are useful only for releasing non-memory resources associated with an object during a long-running program. For example, an os.File object could use a finalizer to close the associated operating system file descriptor when a program discards an os.File without calling Close, but it would be a mistake to depend on a finalizer to flush an in-memory I/O buffer such as a bufio.Writer, because the buffer would not be flushed at program exit.

单个goroutine按顺序运行程序的所有终结器。如果一个终结者必须运行很长时间,它应该通过启动一个新goroutine来完成。

A single goroutine runs all finalizers for a program, sequentially. If a finalizer must run for a long time, it should do so by starting a new goroutine.