-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
executor: fix the issue that pb
memory cannot be released quickly
#10815
Changes from all commits
ff5eb27
b6f5b08
62425a5
abf2a7e
9fe1baa
8b48d20
aad5420
992a6d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2019 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"runtime" | ||
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/domain" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/session" | ||
"github.com/pingcap/tidb/store/mockstore" | ||
) | ||
|
||
var _ = SerialSuites(&testMemoryLeak{}) | ||
|
||
type testMemoryLeak struct { | ||
store kv.Storage | ||
domain *domain.Domain | ||
} | ||
|
||
func (s *testMemoryLeak) SetUpSuite(c *C) { | ||
var err error | ||
s.store, err = mockstore.NewMockTikvStore() | ||
c.Assert(err, IsNil) | ||
s.domain, err = session.BootstrapSession(s.store) | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
func (s *testMemoryLeak) TestPBMemoryLeak(c *C) { | ||
se, err := session.CreateSession4Test(s.store) | ||
c.Assert(err, IsNil) | ||
_, err = se.Execute(context.Background(), "create database test_mem") | ||
c.Assert(err, IsNil) | ||
_, err = se.Execute(context.Background(), "use test_mem") | ||
c.Assert(err, IsNil) | ||
|
||
// prepare data | ||
totalSize := uint64(256 << 20) // 256MB | ||
blockSize := uint64(8 << 10) // 8KB | ||
delta := totalSize / 5 | ||
numRows := totalSize / blockSize | ||
_, err = se.Execute(context.Background(), fmt.Sprintf("create table t (c varchar(%v))", blockSize)) | ||
c.Assert(err, IsNil) | ||
defer func() { | ||
_, err = se.Execute(context.Background(), "drop table t") | ||
c.Assert(err, IsNil) | ||
}() | ||
sql := fmt.Sprintf("insert into t values (space(%v))", blockSize) | ||
for i := uint64(0); i < numRows; i++ { | ||
_, err = se.Execute(context.Background(), sql) | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
// read data | ||
runtime.GC() | ||
allocatedBegin, inUseBegin := s.readMem() | ||
records, err := se.Execute(context.Background(), "select * from t") | ||
c.Assert(err, IsNil) | ||
record := records[0] | ||
rowCnt := 0 | ||
chk := record.NewRecordBatch() | ||
for { | ||
c.Assert(record.Next(context.Background(), chk), IsNil) | ||
rowCnt += chk.NumRows() | ||
if chk.NumRows() == 0 { | ||
break | ||
} | ||
} | ||
c.Assert(rowCnt, Equals, int(numRows)) | ||
|
||
// check memory before close | ||
runtime.GC() | ||
allocatedAfter, inUseAfter := s.readMem() | ||
c.Assert(allocatedAfter-allocatedBegin, GreaterEqual, totalSize) | ||
c.Assert(s.memDiff(inUseAfter, inUseBegin), Less, delta) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check ensures that the memory used in this query can be recycled by the GC scanner. |
||
|
||
se.Close() | ||
runtime.GC() | ||
allocatedFinal, inUseFinal := s.readMem() | ||
c.Assert(allocatedFinal-allocatedAfter, Less, delta) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure that these executors don't allocate much memory when closing. |
||
c.Assert(s.memDiff(inUseFinal, inUseAfter), Less, delta) | ||
} | ||
|
||
func (s *testMemoryLeak) readMem() (allocated, heapInUse uint64) { | ||
var stat runtime.MemStats | ||
runtime.ReadMemStats(&stat) | ||
return stat.TotalAlloc, stat.HeapInuse | ||
} | ||
|
||
func (s *testMemoryLeak) memDiff(m1, m2 uint64) uint64 { | ||
if m1 > m2 { | ||
return m1 - m2 | ||
} | ||
return m2 - m1 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an empirical value, used to judge if memory leak happens.
The difference between
InUseHeap
before and after a query must be less than it.