Fanch
Fanch

Reputation: 21

Question about memory management of jsoncpp

Hello !

I have a question about memory with jsoncpp. I have to load large JSon file (55 MB) in c++ with jsoncpp. I noticed my program use lot of RAM. I tried something, just open, parse and close the JSON file. After the closing of file the memory usage did'nt decrese at all.

I also tried with RapidJson and after returning a lot of memory is freed.

I'm using Linux

I tried this code, which prints statm file in /proc/PID/statm before and after Jsoncpp parse. It prints the memory before, during and after the parse function.

#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();
}

The result is: For jsoncpp

2552 629 516 51 0 188 0
107744 106364 1052 51 0 105380 0
107744 106364 1052 51 0 105380 0

So before the parse, the total memoy used is 2552*4096 +/-= 10 MiB. During and after the function memory usage is strictly the same 107744*4096 +/-= 420 MiB.

For RapidJson:

2552 642 530 51 0 188 0
24275 22871 1056 51 0 21911 0
4140 2780 1056 51 0 1776 0

RapidJson free a lot of memory but not etire.

Jsoncpp should free his memory after returning jsoncpp(), isnt it ? I tried Valgrind with this program (with jsoncpp), and there is no memory leak.

==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)

Why jsoncpp does not free memory in returning the jsoncpp() function ? After parsing i dont need to acces the data, hot to clean it ?

Thanks

Upvotes: 2

Views: 857

Answers (0)

Related Questions