有没有办法在Go结构体被回收时释放非托管资源?

huangapple go评论83阅读模式
英文:

Is there a way to release unmanaged resources when a Go struct is collected?

问题

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

type Wrapper struct {
    unmanaged *C.my_c_type
}

而C类型又有以下函数:

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

有没有办法确保在每次Wrapper实例被销毁时调用free_c_type函数?

英文:

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

type Wrapper struct {
    unmanaged *C.my_c_type
}

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

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

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

答案1

得分: 24

你可以使用runtime.SetFinalizer。这允许你在对象超出范围时运行清理函数。不能保证一定会运行。然而,在释放内存时,这并不重要。重要的是对于长时间运行的进程,它很可能保持垃圾的控制。

以下是文档中的一些摘录(整个段落已被删除):

> SetFinalizer将与x关联的终结器设置为f。当垃圾收集器发现一个不可达的块并且有一个关联的终结器时,它会清除关联并在一个单独的goroutine中运行f(x)。这使得x再次可达,但现在没有关联的终结器。假设不再调用SetFinalizer,下次垃圾收集器发现x不可达时,它将释放x。
>
> x的终结器计划在x变得不可达后的某个任意时间运行。不能保证终结器会在程序退出之前运行,因此它们通常只在长时间运行的程序中用于释放与对象关联的非内存资源。例如,一个os.File对象可以使用终结器在程序丢弃os.File而不调用Close时关闭关联的操作系统文件描述符,但依赖终结器来刷新内存中的I/O缓冲区(如bufio.Writer)是错误的,因为缓冲区在程序退出时不会被刷新。
>
> 一个goroutine按顺序运行程序的所有终结器。如果一个终结器必须长时间运行,它应该通过启动一个新的goroutine来实现。

英文:

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 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.
>
> 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.
>
> 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.

huangapple
  • 本文由 发表于 2012年8月5日 03:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/11811551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定