-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What's the use of finalizer.ch? #6
Comments
the original code is from uber's blog article, I think any struct is ok~ |
can it just simply be |
Thanks guys, I have done some experiments after your questions, I think this channel is useless, you can reproduce the result by following demo: package main
import (
"runtime"
"time"
)
type finalizer struct {
ref *finalizerRef
}
type finalizerRef struct {
parent *finalizer
}
// default GOGC = 100
var previousGOGC = 100
// don't trigger err log on every failure
var failCounter = -1
func finalizerHandler(f *finalizerRef) {
println("in final handler")
runtime.SetFinalizer(f, finalizerHandler)
}
type person struct {
data []byte
}
// NewTuner
// set useCgroup to true if your app is in docker
// set percent to control the gc trigger, 0-100, 100 or upper means no limit
//
// modify default GOGC value in the case there's an env variable set.
func getFinal() * finalizer {
var f = &finalizer{
}
f.ref = &finalizerRef{parent: f}
runtime.SetFinalizer(f.ref, finalizerHandler)
f.ref = nil
return f
}
func main() {
f := getFinal()
println(f)
for {
var x = person{make([]byte, 100000)}
time.Sleep(time.Millisecond*40)
println(x.data[0])
}
}
you should see every gc trigger will print a line of 'in final handler' |
Avoid finalizer being collected by GC? why use chan time.Time?
The text was updated successfully, but these errors were encountered: