Reputation: 5890
I used unordered_map, the key is string, the value is a class IpsegInfo. I read a file and used the file data to update the value in map, the map size always same but the map space in memory is getting bigger and bigger.
My code update the map from file every 30 minutes, the file has 10million lines and the length never changed, only the data would be change. So my map length is always same as 10 million as well. The file physical size is 500MB, the unordered_map memory size is 4GB after first time load, but after a few hours the map size become 50GB and it's getting bigger continually, I think I should have somewhere did memory leak.
My code as following:
class IpsegInfo {
public:
string country;
string province;
string isp;
string asn;
IpsegInfo();
IpsegInfo(string country, string province, string isp, string asn);
~IpsegInfo();
};
//one thread is doing the map refresh. other threads read the map for quick access.
unordered_map<string, IpsegInfo> IpsegMap::map;
void refresh(){
while(1){
string line;
string delim = ",";
ifstream ipsegFile("/opt/test/ipseg.txt");
if(ipsegFile.is_open()){
while (getline(ipsegFile,line) ){
trim(line);
vector<string> ss = comUtil.split(line,delim);
IpsegInfo *info = new IpsegInfo(ss[1],ss[2],ss[3],ss[4]);
map[ss[0]] = *info;
}
}
ipsegFile.close();
sleep(60*30);
}
}
Upvotes: 0
Views: 359
Reputation: 571
IpsegInfo *info = new IpsegInfo(ss[1],ss[2],ss[3],ss[4]);
map[ss[0]] = *info;
Here you create a IpsegInfo
using new. That means that you should also delete it using delete
.
Instead you simply override it, creating a memory leak. Use RAII to make your data automatically managed:
IpsegInfo info = IpsegInfo(ss[1],ss[2],ss[3],ss[4]); // no pointers!
map[ss[0]] = info;
Upvotes: 4