Skip to content

Commit

Permalink
util: fix the issue that memory usage not released after tracker Deta…
Browse files Browse the repository at this point in the history
…ch (#34592)

ref #34571
  • Loading branch information
wshwsh12 authored May 13, 2022
1 parent ad53ac2 commit 4235034
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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

0 comments on commit 4235034

Please sign in to comment.