Reputation: 31
message LongUserIdSeqIdMapData {
map<int64, int32> userid_seqid = 1;
map<int32, int64> sedid_userid = 2; }
void GetUserIdSeqId(const std::string &user_id_seq_id_file) {
std::ifstream infile(user_id_seq_id_file);
infile.seekg(0, infile.end);
size_t length = infile.tellg();
infile.seekg(0, infile.beg);
auto *buffer = new char[length];
infile.read(buffer, length);
auto long_user_id_seq_id_map = new com::jaymz::poseidon::LongUserIdSeqIdMapData::LongUserIdSeqIdMapData();
if (!(*long_user_id_seq_id_map).ParseFromArray(buffer, length)) {
std::cout << "Parse user_id_seq_id_file Fail, Please Check Your File!" << std::endl;
} else {
std::cout << "Parse user_id_seq_id_file Success" << std::endl;
}
delete[] buffer;
delete long_user_id_seq_id_map;
}
I first write LongUserIdSeqIdMapData data to a file, then parse it from the file by call function GetUserIdSeqId, I found the program cccupied 190M physical memory when execute GetUserIdSeqId, but after finish execute GetUserIdSeqId, the program still cccupied 190M physical memory as no memory is freed, I don't know why.
Upvotes: 3
Views: 7390
Reputation: 1433
There is a function that free the memory in C++:
google::protobuf::ShutdownProtobufLibrary();
Shut down the entire protocol buffers library, deleting all static-duration objects allocated by the library or by generated .pb.cc files.
There are two reasons you might want to call this:
- You use a draconian definition of "memory leak" in which you expect every single malloc() to have a corresponding free(), even for objects which live until program exit.
- You are writing a dynamically-loaded library which needs to clean up after itself when the library is unloaded.
It is safe to call this multiple times. However, it is not safe to use any other part of the protocol buffers library after ShutdownProtobufLibrary() has been called.
This should solve your problem!
Upvotes: 5
Reputation: 12176
It is quite common that C/C++ memory allocation functions don't return the freed memory to the operating system, but keep it in their own list to satisfy future allocations.
For example, if you use new
to allocate a 190 MB array, then delete
it, it is quite likely that operating system tools will still show that the program has used 190 MB. However, to see if the function is really leaking memory, you can run it twice. If it leaks memory, after the second use it will take 380 MB - but if it has freed the memory, it will reuse the same 190 MB.
Often this behaviour is configurable in your C++ runtime. But the defaults have been selected so that they are most suitable for common use cases.
Upvotes: 1