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

后端 未结 1 837
臣服心动
臣服心动 2021-02-20 01:10

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 f

相关标签:
1条回答
  • 2021-02-20 01:23

    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.

    0 讨论(0)
提交回复
热议问题