Coder Social home page Coder Social logo

What's the use of finalizer.ch? about gogctuner HOT 3 OPEN

cch123 avatar cch123 commented on August 24, 2024
What's the use of finalizer.ch?

from gogctuner.

Comments (3)

cch123 avatar cch123 commented on August 24, 2024 2

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])
	}
}

GODEBUG=gctrace=1 go run ./tuner.go

you should see every gc trigger will print a line of 'in final handler'

from gogctuner.

cch123 avatar cch123 commented on August 24, 2024

Avoid finalizer being collected by GC? why use chan time.Time?

the original code is from uber's blog article, I think any struct is ok~

from gogctuner.

zjx20 avatar zjx20 commented on August 24, 2024

can it just simply be runtime.KeepAlive(f.parent) or just be empty (do nothing)?

from gogctuner.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.