test code is as below. Memory usage continue to increase when starting new thread and loading data.
#include <cassert>
#include <vector>
#include <iostream>
#include <string>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include "leveldb/db.h"
#include "leveldb/cache.h"
using namespace std;
leveldb::DB* pldb;
leveldb::Cache* pblock_cache = NULL;
void* loadhotThread(void* arg){
cout<<"new thread start"<<endl;
int cnt = 0;
leveldb::Iterator* it = pldb->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst();
it->Valid();
it->Next()) {
it->value()[0];
cnt++;
}
delete it;
cout<<cnt<<" data load;"<<endl;
//thread not exit
cout<<"thread begin sleep"<<endl;
while(1){
sleep(10);
}
printf("thread exit\n");
fflush(stdout);
pthread_detach(pthread_self());
return NULL;
}
int loadhotMultiThread(int argc, char **argv)
{
int cnt = 0;
while(cnt<10){
pthread_t pid;
pthread_create(&pid, NULL, loadhotThread, NULL);
//wait the thread load data
sleep(60);
cnt++;
}
cout<<"main thread sleep"<<endl;
while(1){
sleep(10);
}
return 0;
}
int main(int argc, char **argv)
{
if (argc != 2){
cout<<"usage:"<<argv[0]<<" /path/to/leveldb/data/"<<endl;
return -1;
}
string dir = argv[1];
leveldb::Options options;
//2G cache size
uint64_t a = 2000000000;
options.block_cache = leveldb::NewLRUCache(a);
pblock_cache = options.block_cache;
options.max_open_files = 65535;
options.create_if_missing = true;
options.block_size = 1024*32;
options.write_buffer_size = 128000000;
leveldb::Status s = leveldb::DB::Open(options, dir, &pldb);
loadhotMultiThread(argc, argv);
delete pldb;
delete pblock_cache;
return 0;
}
//g++ test_levelcache.cpp /data/pavelliu/extern_source/leveldb-1.14.0/libleveldb.a -o test_levelcache -I /data/pavelliu/extern_source/leveldb-1.14.0/include/ -L/data/pavelliu/local/lib/ -I/data/pavelliu/local/include/ -g -lsnappy -lpthread