Skip to content

Why nlohmann does not release memory #1924

Description

@mohammad-nazari

We have two problems:
1- Why nlohmann use huge memory to parse data
2- After call parser in a function locally like below code it does not release memory. My JSON data size is about 8MB and the parser use more then 50MB for parsing. I parsed this JSON data 10 times and memory usage goes up to 600MB and after the function is finished memory did not released.

    GenerateNlohmann() {
      std::string filePath{FILE_ADDRESS};
      std::ifstream iFile(filePath.c_str(), std::ios::in);
      std::string data{};
      if (iFile.is_open()) {
        data = std::string((std::istreambuf_iterator<char>(iFile)),
                           std::istreambuf_iterator<char>()); // About 8MB size
        iFile.close();
      }
      if (!data.empty()) {
        nlohmann::json json = nlohmann::json::parse(data); // Use memory about 50MB
        std::vector<nlohmann::json> jsons{};
        for (int i = 0; i < 10; ++i) {
          nlohmann::json j = nlohmann::json::parse(data);
          jsons.emplace_back(j);
        }
        while (!jsons.empty()) {
          jsons.pop_back();
        }
      }
    }

        int main() {
          GenerateNlohmann();

    // Now memory usage is about 600MB
          std::cout << "Input a numberto exit" << std::endl;
          int i;
          std::cin >> i;

          return 0;
        }

Our platform is Ubuntu 18.04, CMake 3.15.3, g++ 7.4.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions