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

for 循环里面调用 defer cancel() 的调用导致内存泄露,程序OOM #15

Open
ymakedaq opened this issue Jun 26, 2021 · 1 comment

Comments

@ymakedaq
Copy link

ymakedaq commented Jun 26, 2021

代码段 parser.go

	for {
		if p.cfg.StopNever {
			ctx = context.Background()
		} else {
			ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
			defer cancel()  // 此处在 每次循环中并不会被调用 
		}
		// e, err := s.GetEvent(context.Background())
		e, err := s.GetEvent(ctx)
		if err != nil {
			// Try to output all left events
			// events := s.DumpEvents()
			// for _, e := range events {
			//  // e.Dump(os.Stdout)
			//  log.Info("===============")
			//  log.Info(e.Header.EventType)
			// }
			log.Errorf("Get event error: %v\n", err)
			break
		}

pprof head

` ``
go tool pprof http://127.0.0.1:9999/debug/pprof/heap
Fetching profile over HTTP from http://127.0.0.1:9999/debug/pprof/heap
Saved profile in /root/pprof/pprof.bingo2sql.alloc_objects.alloc_space.inuse_objects.inuse_space.019.pb.gz
File: bingo2sql
Type: inuse_space
Time: Jun 26, 2021 at 10:27am (UTC)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 2.99GB, 97.40% of 3.07GB total
Dropped 40 nodes (cum <= 0.02GB)
Showing top 10 nodes out of 28
flat flat% sum% cum cum%
1.04GB 33.82% 33.82% 1.11GB 36.04% context.WithDeadline
0.94GB 30.54% 64.36% 0.94GB 30.54% context.(*cancelCtx).Done.
0.76GB 24.77% 89.13% 0.76GB 24.77% runtime.systemstack.
0.06GB 1.94% 91.06% 0.07GB 2.22% time.AfterFunc
0.04GB 1.38% 92.45% 0.10GB 3.10% github.com/hanchuanchuan/go-mysql/replication.(*RowsEvent).Decode
0.04GB 1.32% 93.77% 0.16GB 5.10% github.com/hanchuanchuan/go-mysql/replication.(*BinlogParser).parseEvent
0.04GB 1.30% 95.07% 0.05GB 1.67% github.com/hanchuanchuan/go-mysql/packet.(*Conn).ReadPacket
0.03GB 1.06% 96.13% 0.03GB 1.06% time.goFunc
0.02GB 0.73% 96.86% 0.05GB 1.72% github.com/hanchuanchuan/go-mysql/replication.(*RowsEvent).decodeRows
0.02GB 0.54% 97.40% 0.02GB 0.54% github.com/hanchuanchuan/go-mysql/replication.(*BinlogParser).newRowsEvent

@ymakedaq
Copy link
Author

可以改成以下方式

	var ctx context.Context
	var cancel context.CancelFunc
	for {
		if p.cfg.StopNever {
			ctx = context.Background()
		} else {
			ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
			//defer cancel()
		}
		// e, err := s.GetEvent(context.Background())
		e, err := s.GetEvent(ctx)
		if err != nil {
			// Try to output all left events
			// events := s.DumpEvents()
			// for _, e := range events {
			//  // e.Dump(os.Stdout)
			//  log.Info("===============")
			//  log.Info(e.Header.EventType)
			// }
			cancel()
			log.Errorf("Get event error: %v\n", err)
			break
		}
		cancel()

@ymakedaq ymakedaq changed the title context.WithTimeout 的调用导致内存泄露,程序OOM for 循环里面调用 defer cancel() 的调用导致内存泄露,程序OOM Jun 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant