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

HELP! There is a memory leak in the code?! #2837

Closed
HuberyQ opened this issue Jun 23, 2021 · 11 comments
Closed

HELP! There is a memory leak in the code?! #2837

HuberyQ opened this issue Jun 23, 2021 · 11 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@HuberyQ
Copy link

HuberyQ commented Jun 23, 2021

Discussed in https://github.com/nlohmann/json/discussions/2836

Originally posted by HuberyQ June 23, 2021
Sorry to attract you with such a scary title. First of all, I would like to thank you for this work!

I found that after parsing the. MSG file, I got the JSON object successfully, but the CPU memory was not released until the end of the program! Is there a mistake in the way I use it? Can you manually free up the consumed memory?

The size of the. MSG file is about 24MB, but the memory consumption of the parsing process is about 0.4G! Here is the test source code:

void test_json(string path)
{
    std::ifstream ifs(path, std::ios::in | std::ios::binary);
    if (!ifs.is_open())
    {
        throw std::runtime_error("cannot load the file at " + path);
    }

    std::vector<uint8_t> msgpack;
    while (true)
    {
        uint8_t buffer;
        ifs.read(reinterpret_cast<char*>(&buffer), sizeof(uint8_t));
        if (ifs.eof())
        {
            break;
        }
        msgpack.push_back(buffer);
    }
    ifs.close();

    const auto json = nlohmann::json::from_msgpack(msgpack);
    cout << sizeof(json) / 1024.0 / 1024.0 << endl;
}
@HuberyQ
Copy link
Author

HuberyQ commented Jun 23, 2021

I have two questions: (1) How to release the memory consumption that has not been released? (2) Why is memory consumption so huge?
Hope to get help from you or other people, thank you!

@HuberyQ
Copy link
Author

HuberyQ commented Jun 23, 2021

This is the .msg file: 1.msg

@HuberyQ
Copy link
Author

HuberyQ commented Jun 23, 2021

I didn't test with Valgrind or ASAN...I just monitoring CPU through my terminal.
Ubuntu 20.04 / C++11

@nlohmann
Copy link
Owner

  • There is no memory leak in the library - you can compile your code with ASAN or run it with Valgrind. All memory acquired for json is release once it goes out of scope.

  • The memory consumption of the library is due to the overhead described in the README:

    Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte).

    Your JSON value contains of a lot of numbers, and hence this overhead adds up.

Note the overhead is much smaller if you compile your binary in release mode.

@HuberyQ
Copy link
Author

HuberyQ commented Jun 23, 2021

* There is no memory leak in the library - you can compile your code with ASAN or run it with Valgrind. All memory acquired for `json` is release once it goes out of scope.

* The memory consumption of the library is due to the overhead described in the README:
  > Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte).
  
  
  Your JSON value contains of a lot of numbers, and hence this overhead adds up.

Note the overhead is much smaller if you compile your binary in release mode.

Thank you for your reply! But when the test_json function exits, the CPU memory growth caused by it is not released! You can use the data and code above to reproduce it. We need your help!

@nlohmann
Copy link
Owner

I cannot reproduce this:

image

The library releases all memory, there are no memory leaks. Can you please try to run your code with Valgrind or ASAN?

@gregmarr
Copy link
Contributor

For an explanation of why memory freed by the application isn't returned to the OS: https://stackoverflow.com/questions/45538993/why-dont-memory-allocators-actively-return-freed-memory-to-the-os

@HuberyQ
Copy link
Author

HuberyQ commented Jun 25, 2021

For an explanation of why memory freed by the application isn't returned to the OS: https://stackoverflow.com/questions/45538993/why-dont-memory-allocators-actively-return-freed-memory-to-the-os

Thanks! It really helps a lot!

@HuberyQ
Copy link
Author

HuberyQ commented Jun 25, 2021

I cannot reproduce this:

image

The library releases all memory, there are no memory leaks. Can you please try to run your code with Valgrind or ASAN?

I tested on different systems:
ubuntu16:It's normal and ok!
ubuntu18:The memory was not released!

Then I refered to #1924 , and it worked! I guess that different versions of the glibc lead to different memory recovery mechanisms.

Thanks a lot!
This library is really good to use!

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 25, 2021
@nlohmann
Copy link
Owner

Do you need further assistance with this issue?

@HuberyQ
Copy link
Author

HuberyQ commented Jun 25, 2021

Do you need further assistance with this issue?

Nope.Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants