valgrind でコードをチェックしましたが、メモリ リークはありません。しかし、私は「トップ」を使用してメモリを表示します。「削除」が呼び出された後、295MBのメモリが必要です。
「pmap -x」を使用してメモリを確認します。ほとんどのメモリ コストは [anon] によるものです。
Address Kbytes RSS Dirty Mode Mapping
0000000001114000 301672 301588 301588 rw--- [ anon ]
メモリが解放された理由はわかりませんが、それでも 295MB かかります。理由を知っている人はいますか?
#include <map>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
void test8() {
map<string, map<string,string>* > m;
for (int i = 1; i < 10000; i++) {
map<string, string>* p = new map<string, string>();
for (int j = 1; j < 100; j++) {
(*p)[string(j,'a')] = string(j,'a');
}
m[string(i,'a')] = p;
}
map<string, map<string,string>* >::iterator it;
for (it = m.begin(); it != m.end(); it++) {
it->second->clear();
delete it->second;
}
m.clear();
cout << "free done" << endl;
}
int main(int argc, char**argv) {
test8();
getchar();
}