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

util: fix the issue that memory usage not released after tracker Detach #34592

Merged
merged 7 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions util/memory/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ func reArrangeFallback(a ActionOnExceed, b ActionOnExceed) ActionOnExceed {

// SetLabel sets the label of a Tracker.
func (t *Tracker) SetLabel(label int) {
parent := t.getParent()
t.Detach()
t.label = label
if parent != nil {
t.AttachTo(parent)
}
}

// Label gets the label of a Tracker.
Expand All @@ -229,6 +234,10 @@ func (t *Tracker) Label() int {
// already has a parent, this function will remove it from the old parent.
// Its consumed memory usage is used to update all its ancestors.
func (t *Tracker) AttachTo(parent *Tracker) {
if parent.isGlobal {
t.AttachToGlobalTracker(parent)
return
}
oldParent := t.getParent()
if oldParent != nil {
oldParent.remove(t)
Expand Down
12 changes: 12 additions & 0 deletions util/memory/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func TestSetLabel(t *testing.T) {
require.Equal(t, 0, len(tracker.mu.children))
}

func TestSetLabel2(t *testing.T) {
tracker := NewTracker(1, -1)
tracker2 := NewTracker(2, -1)
tracker2.AttachTo(tracker)
tracker2.Consume(10)
require.Equal(t, tracker.BytesConsumed(), int64(10))
tracker2.SetLabel(10)
require.Equal(t, tracker.BytesConsumed(), int64(10))
tracker2.Detach()
require.Equal(t, tracker.BytesConsumed(), int64(0))
}

func TestConsume(t *testing.T) {
tracker := NewTracker(1, -1)
require.Equal(t, int64(0), tracker.BytesConsumed())
Expand Down