Skip to content
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

GC test found very strange, do not understand #40734

Closed
ruyi789 opened this issue Aug 12, 2020 · 3 comments
Closed

GC test found very strange, do not understand #40734

ruyi789 opened this issue Aug 12, 2020 · 3 comments

Comments

@ruyi789
Copy link

@ruyi789 ruyi789 commented Aug 12, 2020

is the GC bug


package main

import (
	"fmt"
	"runtime"
	"time"
	"unsafe"
)

type ast struct {
	a int
}

func (a *ast) free() {
	cnt++
	fmt.Println("ast_free", unsafe.Pointer(a))
}

type bst struct {
	a *ast
	b **bst
}

func (b *bst) free() {
	cnt++
	fmt.Println("bst_free", unsafe.Pointer(b))
}

var cnt int

func testfreeCall2() {
	a := new(ast)
	b := new(bst)
	b.b = &b
	runtime.SetFinalizer(a, (*ast).free)
	runtime.SetFinalizer(b, (*bst).free)

	b.a = new(ast)                         //Try to block this
	runtime.SetFinalizer(b.a, (*ast).free) //Try to block this

	fmt.Println("testfreeCall2__", unsafe.Pointer(a), unsafe.Pointer(b), unsafe.Pointer(b.a))
}
func testfreeCall1() {
	a := new(ast)
	b := new(bst)

	runtime.SetFinalizer(a, (*ast).free)
	runtime.SetFinalizer(b, (*bst).free)
	b.a = new(ast)
	runtime.SetFinalizer(b.a, (*ast).free)

	fmt.Println("testfreeCall1__", unsafe.Pointer(a), unsafe.Pointer(b), unsafe.Pointer(b.a))
}

func main() {

	//testfreeCall1() //+3
	testfreeCall2() //+1
	//testfreeCall2() //+1
	testfreeCall1() //+3

	time.Sleep(1000 * 1000 * 100)
	for i := 0; i < 5; i++ {
		runtime.GC()
		fmt.Println("status:", cnt, i)
		time.Sleep(1000 * 1000 * 100)
	}

	fmt.Println("exit")
}

@martisch
Copy link
Contributor

@martisch martisch commented Aug 12, 2020

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

See https://github.com/golang/go/wiki/Questions for ressources to ask questions.

If you want to make a bug report please fill the bug template that is presented when creating a new github issue.

@martisch martisch closed this Aug 12, 2020
@ruyi789
Copy link
Author

@ruyi789 ruyi789 commented Aug 12, 2020

is the GC bug

@randall77
Copy link
Contributor

@randall77 randall77 commented Aug 12, 2020

If you think there's a bug, please fill out the new issue template. In particular, what did you do? What did you expect to see? What did you see instead?
(If you have that, you can post it here and reopen this issue.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.