Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse json OOM #4514

Open
2 tasks done
ddxxsx opened this issue Nov 25, 2024 · 5 comments
Open
2 tasks done

parse json OOM #4514

ddxxsx opened this issue Nov 25, 2024 · 5 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@ddxxsx
Copy link

ddxxsx commented Nov 25, 2024

Description

I have a string, which is about 10MB in memory. However, when I use json::parse(), the memory cost is 1.6GB.
I wonder if it's a bug?

Reproduction steps

A long string with chinese charater encoded with utf-8.
call
json j = json::parse(str);
can trigger this bug.

Expected vs. actual results

lower memory cost.

Minimal code example

No response

Error messages

No response

Compiler and operating system

windows

Library version

branch: develop, commit hash: ee32bfc

Validation

@nlohmann
Copy link
Owner

Please share the input to check. Please also make sure you compile in release mode.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Nov 25, 2024
@ddxxsx
Copy link
Author

ddxxsx commented Nov 25, 2024

The input file is too large to upload(github limit 25MB), but the file is 64MB large.
The release mode costs less memory(600MB), while debug mode costs almost 1GB memory.

My code is like this:

ifstream file("C:\\input.txt");
std::stringstream buffer;
buffer << file.rdbuf();

std::string content = buffer.str();
json j = json::parse(content);

Is there a way to upload the input file here?

@nlohmann
Copy link
Owner

I would assume using xz would make the file small enough.

@ddxxsx
Copy link
Author

ddxxsx commented Nov 26, 2024

input.zip

Github doesn't support xz file, I attach a zip file instead.

Memory cost:
image

@nlohmann
Copy link
Owner

Screenshot 2024-11-27 at 18 12 00

I see 250 MB for that file. Not sure what your compiler is doing there.

My code for reference:

#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>

int main() {
	std::ifstream f("/Users/niels/Downloads/input.txt");
	auto x = nlohmann::json::parse(f);

	 do  {
	 	std::cout << '\n' << "Press a key to continue...";
	 } while (std::cin.get() != '\n');
}
g++ -I include -O3 -DNDEBUG=1 -std=c++11 parse.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

2 participants