How important is it to call Close()
sluongng opened this issue · 4 comments
I see the exposed API included Close() in almost all objects. But most documented examples and tests dont use Close() at all.
So how important is it to call Close() when using this lib? 🤔
Hi @sluongng !
The library would call Close
automatically when an object becomes unreachable and deallocated by go's garbage collector. To do this we are using runtime.SetFinalizer.
The important line from the documentation:
The finalizer is scheduled to run at some arbitrary time after the program can no longer reach the object to which obj points.
Because cleaning doesn't happen immediately when you process a lot of data you might run out of memory or your program will just consume more memory than desired. For such use cases, we export Close
method so that developers have more control and can optimize deallocation.
For most use cases, though, it is not necessary, GC and go runtime will do the job just fine.
Does it answer your question?
Ah thanks, that clarified things quite a lot.
I would suggest providing this information as part of the API doc. 🙏
Agree. I will be happy to accept a pull request with improved documentation!