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

memory leak in mockstore #18048

Closed
GMHDBJD opened this issue Jun 16, 2020 · 21 comments · Fixed by #28027
Closed

memory leak in mockstore #18048

GMHDBJD opened this issue Jun 16, 2020 · 21 comments · Fixed by #28027
Assignees
Labels
component/store help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. severity/moderate sig/transaction SIG:Transaction type/bug The issue is confirmed as a bug.

Comments

@GMHDBJD
Copy link
Contributor

GMHDBJD commented Jun 16, 2020

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

package main

import (
	"os"
	"runtime/pprof"

	"github.com/pingcap/tidb/session"
	. "github.com/pingcap/tidb/store/mockstore"
)

func testMock() {
	opts := []MockTiKVStoreOption{WithStoreType(MockTiKV)}
	store, _ := NewMockStore(opts...)
	defer store.Close()

	dom, _ := session.BootstrapSession(store)
	defer dom.Close()
}

func main() {
	for i := 0; i < 10; i++ {
		testMock()
	}
	f, _ := os.Create("heap_profile")
	pprof.WriteHeapProfile(f)
}

2. What did you expect to see? (Required)

free the memory when dom.Close()

3. What did you see instead (Required)

out

4. Affected version (Required)

tidb master

5. Root Cause Analysis

@GMHDBJD GMHDBJD added the type/bug The issue is confirmed as a bug. label Jun 16, 2020
@g1eny0ung
Copy link

/label component/store

@tiancaiamao
Copy link
Contributor

Yes, I can confirm the memory leak.
It's caused by some global variable references, but I do not know which global variable.

@tiancaiamao
Copy link
Contributor

Those are the global variables of the test program.
I use dlv attach to get them.

global.LOG

@csuzhangxc
Copy link
Member

Most of global variables will not increase memory usage, I think we can just let them stay there.

But some of them may increase memory usage after repeated NewMockStore/BootstrapSession/Close, I think we should fix them.

@tiancaiamao
Copy link
Contributor

The global variable itself does not increase memory usage, but the may reference some other variable and make the variable can not be released by GC, thus leak.

@tiancaiamao
Copy link
Contributor

I find one place of the leak

var globalColumnAllocator = newLocalSliceBuffer(1024)
@qw4990

@csuzhangxc
Copy link
Member

@tiancaiamao @qw4990 Is anyone still working on this?

@tiancaiamao
Copy link
Contributor

No progress on this issue. @csuzhangxc
I've tried my best but found it's too difficult to check all the global variable references.

@csuzhangxc
Copy link
Member

@tiancaiamao can you fix the one you found above first?

var globalColumnAllocator = newLocalSliceBuffer(1024)

@qw4990 qw4990 self-assigned this Aug 26, 2020
@csuzhangxc
Copy link
Member

ping anyone

@jebter jebter added the sig/transaction SIG:Transaction label Nov 16, 2020
@qw4990
Copy link
Contributor

qw4990 commented Dec 1, 2020

Sorry for my late reply. Could you please give me your go.mod? I cannot reproduce this problem now. @csuzhangxc

@qw4990
Copy link
Contributor

qw4990 commented Dec 1, 2020

Since this problem only occurs in mockstore, its severity is degraded to minor.

@GMHDBJD
Copy link
Contributor Author

GMHDBJD commented Dec 2, 2020

test.tar.gz
I reproduce with master tidb.(both mocktikv and unistore)

@zhangysh1995
Copy link

/pick-up

@ti-challenge-bot
Copy link

It is not a pickable issue!

Details

Tip : If you want this issue to be picked, you need to add a challenge-program label to it.

Warning: None

@zhangysh1995
Copy link

@ichn-hu Could you please add a label for the bug fixing challenge that I could pick up this issue?

@wjhuang2016 wjhuang2016 added the help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. label Jun 17, 2021
@wjhuang2016
Copy link
Member

@ichn-hu Could you please add a label for the bug fixing challenge that I could pick up this issue?

You can fire a PR to fix it directly.

@GMHDBJD
Copy link
Contributor Author

GMHDBJD commented Sep 7, 2021

Ping

@lance6716
Copy link
Contributor

lance6716 commented Sep 13, 2021

found that domap still holds a reference to Domain after Domain is closed (the key is UUID), but after cleaning the map the memory still can't decrease

tidb/session/tidb.go

Lines 103 to 105 in 7755d25

domap = &domainMap{
domains: map[string]*domain.Domain{},
}

@sleepymole
Copy link
Contributor

sleepymole commented Sep 13, 2021

I found statisticsList will always hold the statistics which is probably a *ddl.ddl.

func RegisterStatistics(s Statistics) {
statisticsListLock.Lock()
statisticsList = append(statisticsList, s)
statisticsListLock.Unlock()
}

After clear domap and comment the follow two lines, the memory won't increase all the time.

variable.RegisterStatistics(handle)

tidb/ddl/ddl.go

Line 386 in c26038b

variable.RegisterStatistics(d)

@github-actions
Copy link

Please check whether the issue should be labeled with 'affects-x.y' or 'backport-x.y.z',
and then remove 'needs-more-info' label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/store help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. severity/moderate sig/transaction SIG:Transaction type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.