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

Reserve存在重大内存泄漏 #2280

Open
lxb320124 opened this issue May 20, 2024 · 0 comments
Open

Reserve存在重大内存泄漏 #2280

lxb320124 opened this issue May 20, 2024 · 0 comments

Comments

@lxb320124
Copy link

lxb320124 commented May 20, 2024

先写个小例子:
for (int i = 0; i < 10000; i++)
{
std::string value = "hello " + std::to_string(rand());
std::string key = "/data/a/b/c/" + std::to_string(i) + "/testValue";
Pointer(key).Set(doc, value);
}
当使用这段代码测试时,内存会直接增长800多M,如果记录数更多的话,内存增长更加可怕 。
排查原因:
ValueType& Pointer::Create 函数里面:
if (v->IsArray()) {
if (t->index >= v->Size()) {
v->Reserve(t->index + 1, allocator);
while (t->index >= v->Size())
v->PushBack(ValueType().Move(), allocator);
exist = false;
}
v = &((*v)[t->index]);
}
因为上面每次增长一条记录,导致这里 v->Reserve 一直在执行,内存暴增。
if (v->IsArray()) {
if (t->index == v->Size()) {
v->PushBack(ValueType().Move(), allocator);
exist = false;
}
else if (t->index > v->Size()) {
v->Reserve(t->index + 1, allocator);
while (t->index >= v->Size())
v->PushBack(ValueType().Move(), allocator);
exist = false;
}
v = &((*v)[t->index]);
}
这里把代码修改一下,当 t->index == v->Size() 时,不使用Reserve,直接追加一条数据。
这样修改后,上面的示例内存大概只有30M左右,就正常了。

这里只是回避了 Reserve 内存泄漏的问题,具体如果修改这个函数,需要进一步研究。

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