简体   繁体   English

收集Go结构时,是否可以释放非托管资源?

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

I have a pointer to a C type wrapped by a Go struct, like so: 我有一个指向由Go结构包裹的C类型的指针,如下所示:

type Wrapper struct {
    unmanaged *C.my_c_type
}

The C type, in turn, has the following functions: C类型又具有以下功能:

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? 有没有一种方法可以确保在Wrapper实例完成时调用free_c_type

You can use runtime.SetFinalizer . 您可以使用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. SetFinalizer将与x关联的终结器设置为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. 当垃圾收集器找到带有关联的终结器的无法访问的块时,它将清除该关联并在单独的goroutine中运行f(x)。 This makes x reachable again, but now without an associated finalizer. 这使得x再次可访问,但是现在没有关联的终结器。 Assuming that SetFinalizer is not called again, the next time the garbage collector sees that x is unreachable, it will free x. 假设没有再次调用SetFinalizer,则下次垃圾回收器看到x不可达时,它将释放x。

The finalizer for x is scheduled to run at some arbitrary time after x becomes unreachable. x的终结器计划在x变得不可访问之后的任意时间运行。 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. 例如,当程序在不调用Close的情况下丢弃os.File时,os.File对象可以使用终结器关闭关联的操作系统文件描述符,但是依靠终结器来刷新内存中的I将是一个错误。 / O缓冲区,例如bufio.Writer,因为该缓冲区不会在程序退出时刷新。

A single goroutine runs all finalizers for a program, sequentially. 单个goroutine按顺序运行程序的所有终结器。 If a finalizer must run for a long time, it should do so by starting a new goroutine. 如果终结器必须运行很长时间,则应通过启动新的goroutine来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有其他替代概念可用于处理垃圾收集语言中的非托管资源? - Are there any alternative concepts for handling unmanaged resources in garbage collected languages? 当共享值超出范围时,如何处置/释放/“完成”非托管资源 - How to dispose/release/“finalize” unmanaged resources when a shared value gets out of scope C#在dll中调用go函数,运行在.Net Core中,go分配的非托管内存会被垃圾回收吗? - C# calls go function within dll, and it's running in .Net Core, will the unmanaged memory allocated by go gets Garbage Collected? 当对象被垃圾收集时,是否释放了非处置资源? - Are non-disposed resources freed when an object is garbage collected? .Net 中的托管和非托管资源 - Managed and Unmanaged resources in .Net 分配非托管资源的地方 - Where unmanaged resources are allocated .NET:有什么方法可以告诉对象何时处理/垃圾回收? - .NET: Any way to tell when an object is disposed/garbage collected? 非托管资源,IDisposable和自定义类型 - Unmanaged Resources, IDisposable and Custom Types 在非托管资源上执行P / Invoke时需要GC.KeepAlive(this)吗? - When GC.KeepAlive(this) is needed when doing P/Invoke on unmanaged resources? 完成课程后如何释放课程的所有资源 - How to release all the resources of a class when I have done with it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM