gpt4 book ai didi

c++ - 京都内阁 TreeDB : memory usage grows uncontrollably until database is closed

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:30:08 25 4
gpt4 key购买 nike

我一直在尝试使用 kyotocabinet TreeDB(MSVC10 构建)并偶然发现了一个奇怪的内存问题:基本上,在每次数据库写入时,内存使用量都会增加,并且在数据库关闭之前不会下降。

测试代码如下所示:

size_t BLOCK_SIZE = 1 << 20; // use 1MB-sized records
char* test_block = new char[BLOCK_SIZE]; // allocate record data

TreeDB db;
db.open("test.db")

// add 5000 records
for (int i = 0; i < 5000; ++i)
{
// at each call, process virtual memory usage increases by 1 MB even though i'm not allocating more memory
// also, as expected, in x86-32 process the whole thing runs out of memory and crashes when iteration counter comes close to 2000
db.set(&i, sizeof(i), test_block, BLOCK_SIZE);
}

db.close(); // memory usage returns to normal here
delete [] test_block;

当然,我可以在添加每条记录时打开/关闭数据库,但这会引入巨大的延迟(大约 1 秒),这对我的任务来说是 Not Acceptable 。另外,这个问题在HashDB中没有出现,但我不能真正使用HashDB,因为我偶尔需要按键顺序访问。

我试过更改调整参数(tune_page_cache、tune_buckets、tune_page),但没有成功。请有人暗示我在这里缺少什么?我需要存储不可预测数量的 100KB-10MB 大小的记录并在 32 位系统上运行它。

最佳答案

也许 db.synchronize() 会有所帮助,因为它应该将缓存数据推送到文件系统,从而释放用户内存。如果是这样,并且您需要一直发生这种情况,请使用可选参数 OAUTOSYNC 调用 open,尽管这可能会大大降低您的速度。

关于c++ - 京都内阁 TreeDB : memory usage grows uncontrollably until database is closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com