英文:
Does Erase deletes heap memory used by element of stl unordered map
问题
我有一个未排序的int和string的unordered_map,其中的值是通过引用插入的。我想要从map中删除一些元素并减少malloc使用的内存,但我看不到内存减少。使用erase能减少malloc内存吗?在erase后会帮助rehash吗?
typedef typename std::unordered_set<N> ChangeObjSet;
ChangeObjSet _changeObjSet;
template <class N>
void MdmNameCacheImplMIWithStripEscape<N>::insertChangedStatus(const N &obj)
{
_changeObjSet.insert(obj);
}
typename ChangeObjSet::iterator it = _changeObjSet.begin();
for (; it != _changeObjSet.end();)
{
auto s = it;
it++;
_changeObjSet.erase(s);
}
英文:
I have an unordered map of int and string into which values are inserted by reference.I want to erase some elements from map and reduce the malloc memory used, but I can't see reduction in memory.
Is it possible to reduce malloc memory using erase? Will rehash help after erase?
typedef typename std::unordered_set<N> ChangeObjSet;
ChangeObjSet _changeObjSet;
template <class N>
void MdmNameCacheImplMIWithStripEscape<N>::insertChangedStatus(const N &obj)
{
_changeObjSet.insert(obj);
}
typename ChangeObjSet::iterator it = _changeObjSet.begin();
for(;it != _changeObjSet.end();)
{
auto s = it;
it++;
_changeObjSet.erase(s);
}
答案1
得分: 2
我正在使用Linux的stat流来获取malloc内存。
操作系统分配给您的进程的内存与您当前使用的内存不一定相同。为每个被删除的对象释放内存回操作系统会非常低效。您可能在进程终止之前看不到分配给进程的内存减少。
英文:
> I am using linux stat stream to get malloc memory
Memory assigned by the OS to your process is not necessarily the same as memory you are using currently. Releasing memory back to the OS for each object that gets deleted would be horribly inefficient. You may see no decrease in memory assigned to the process until it terminates.
答案2
得分: 1
如果地图中存储的是项目本身(而不是指针),在使用erase时它将被删除。
如果在erase之后地图本身缩小,可能取决于您使用的特定地图的实现。
一些实现可能会将先前擦除的表条目重新用于新插入的项目,尽管我认为大多数实现不会这样做。您是否使用STD库?
英文:
In case the item itself is stored in the map (and not a pointer) it will be deleted when using erase.
If the map itself shrinks after erase may depends on the implementation of the specific map you are using.
Some implementations may reuse previously erased table entries for newly inserted items although I think most implementations don't. Are you using STD library?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论