C++ 关于jsoncpp内存管理的问题
你好强> 我有一个关于jsoncpp内存的问题。我必须用JSONCPP在C++中加载大的JSON文件(55 MB)。我注意到我的程序使用了很多RAM。我尝试了一些方法,只是打开、解析和关闭JSON文件。文件关闭后,内存使用量根本没有减少 我也尝试过使用RapidJson,在返回大量内存后被释放 我正在使用Linux 我尝试了这段代码,它在Jsoncpp解析前后在/proc/PID/statm中打印statm文件。 它在解析函数之前、期间和之后打印内存C++ 关于jsoncpp内存管理的问题,c++,memory-management,jsoncpp,C++,Memory Management,Jsoncpp,你好 我有一个关于jsoncpp内存的问题。我必须用JSONCPP在C++中加载大的JSON文件(55 MB)。我注意到我的程序使用了很多RAM。我尝试了一些方法,只是打开、解析和关闭JSON文件。文件关闭后,内存使用量根本没有减少 我也尝试过使用RapidJson,在返回大量内存后被释放 我正在使用Linux 我尝试了这段代码,它在Jsoncpp解析前后在/proc/PID/statm中打印statm文件。 它在解析函数之前、期间和之后打印内存 #include <iostream>
#include <iostream>
#include <json/json.h>
#include <fstream>
#include <unistd.h>
#include <iostream>
#include <json/json.h>
#include <fstream>
#include <unistd.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/document.h>
void printmem()
{
char tmp[128];
std::string t;
sprintf(tmp, "/proc/%d/statm", getpid());
std::ifstream ifs(tmp);
std::getline(ifs, t);
std::cout << t <<"\n";
ifs.close();
}
void jsoncpp()
{
std::ifstream ifs("../../../AlloDB/db.json");
Json::CharReaderBuilder builder;
Json::Value value;
JSONCPP_STRING errs;
Json::parseFromStream(builder, ifs, &value, NULL);
ifs.close();
printmem();
}
void rapid_json()
{
using namespace rapidjson;
std::ifstream ifs("../../../AlloDB/db.json");
std::string t;
IStreamWrapper isw(ifs);
Document d;
d.ParseStream(isw);
printmem();
}
int main(int argc, char** argv)
{
printmem();
//jsoncpp();
rapid_json();
printmem();
}
因此,在解析之前,使用的总内存是2552*4096+/-=10MIB。在函数期间和之后,内存使用量严格相同107744*4096+/-=420 MiB
对于RapidJson:
2552 642 530 51 0 188 0
24275 22871 1056 51 0 21911 0
4140 2780 1056 51 0 1776 0
RapidJson释放了大量内存,但没有释放etire
Jsoncpp应该在返回Jsoncpp()后释放内存,不是吗?我用这个程序(用jsoncpp)尝试了Valgrind,没有内存泄漏
==133628== Memcheck, a memory error detector
==133628== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==133628== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==133628== Command: bin/Debug/db
==133628==
50448 39807 1539 498 0 47403 0
649572 639457 1671 498 0 646527 0
440676 431819 1671 498 0 437631 0
==133628==
==133628== HEAP SUMMARY:
==133628== in use at exit: 0 bytes in 0 blocks
==133628== total heap usage: 5,917,609 allocs, 5,917,609 frees, 601,544,185 bytes allocated
==133628==
==133628== All heap blocks were freed -- no leaks are possible
==133628==
==133628== For lists of detected and suppressed errors, rerun with: -s
==133628== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
为什么jsoncpp在返回jsoncpp()函数时不释放内存?
解析后,我不需要访问数据,热清理它
谢谢
==133628== Memcheck, a memory error detector
==133628== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==133628== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==133628== Command: bin/Debug/db
==133628==
50448 39807 1539 498 0 47403 0
649572 639457 1671 498 0 646527 0
440676 431819 1671 498 0 437631 0
==133628==
==133628== HEAP SUMMARY:
==133628== in use at exit: 0 bytes in 0 blocks
==133628== total heap usage: 5,917,609 allocs, 5,917,609 frees, 601,544,185 bytes allocated
==133628==
==133628== All heap blocks were freed -- no leaks are possible
==133628==
==133628== For lists of detected and suppressed errors, rerun with: -s
==133628== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)