I am wondering how is memory managed when different files are stored in a map of string vectors. I have tried to read different files of 10 mo each to put them in memory and when I check the memory with KSySGuard, the memory appearing is more than twice the memory of my files (~70mo). I give you a code example for it: There is a function readfile():
std::vector<std::string> read_file(std::string& path){
ifstream fichier(path);
std::vector<std::string> fich;
if(fichier){
string ligne;
while(getline(fichier, ligne)){
fich.push_back(ligne);
}
}
fichier.close();
return fich;
}
This function is used in another which is building my map:
std::map<std::string, std::vector<std::string>> buildmap(std::string folder){
std::map<std::string,std::vector<std::string>> evaluations; std::vector<std::string> vecFiles = {"file1","file2","file3"};
for( auto i = 0; i < vecFiles.size(); i++ )
{
std::stringstream strad;
strad <<vecFiles[i] ;
std::string path(folder+vecFiles[i]);
std::vector<std::string> a = read_file(path);
evaluations[strad.str()]=a;
}
return evaluations;
}
So, I do not understand why the memory is so high compared to the files sizes. Is-there a more efficient way to construct this kind of container?