2

I wonder how to free the memory std::unordered_set took?

I think unordered_set::clear() just clear the set without free the memory.

I need to free the unordered_set hold by local function. Each time I called local function, it will consume some memory, so I need to free it at the end of local function.

Thank you

1
  • 2
    Is the unordered_set local to the function? If it is, then its memory will be correctly released at the end of the function without you needing to do anything at all.
    – Mankarse
    Nov 12, 2011 at 3:29

3 Answers 3

2

The usual way to clear the memory of an unordered_set is the same as for any standard container:

set = std::unordered_set<T>()

You can also use:

std::unordered_set<T>().swap(set)

which was the usual way to release the memory from containers in the days before r-value refs.

You should replace T with the type of the objects contained in the set.

6
  • when I do this: std::tr1::unordered_set::swap(_closelist); it is showing'error: 'template<class _Value, class _Hash, class _Pred, class _Alloc> class std::tr1::unordered_set' used without template parameters' and when I do _closelist.swap(std::tr1::unordered_set()); it showserror: missing template arguments before '(' token
    – weeo
    Nov 12, 2011 at 3:24
  • Sorry... I left out part of the example. You will need to supply the template paramater to the unordered_set (see the edit).
    – Mankarse
    Nov 12, 2011 at 3:26
  • so essentially we are assigning the set with a new empty type? I think my memory is not released.....i'm doing <pre><code>std::tr1::unordered_set<std::tr1::array<int, 16> >().swap(_closelist); _closelist = std::tr1::unordered_set<std::tr1::array<int, 16> >();
    – weeo
    Nov 12, 2011 at 3:37
  • I test it with this code, it uses 2.7GB memory but only free to 2.5GBcode//test free set// std::tr1::unordered_set<std::tr1::array<int, 16> >_closelist; for(i =0;i<30000000;i++) { std::tr1::array<int, 16> test = {9,8,3,7,2,1,6,10,5,13,0,4,12,15,14,i}; _closelist.insert(test); } std::tr1::unordered_set<std::tr1::array<int, 16> >().swap(_closelist); printf("hahahahahahhahahaha\n\n\n\n\n\n");code
    – weeo
    Nov 12, 2011 at 3:53
  • You do not need to do both std::tr1::unordered_set<std::tr1::array<int, 16> >().swap(_closelist); and _closelist = std::tr1::unordered_set<std::tr1::array<int, 16> >();. Either on its own is fine. You are right though. This is not guaranteed to result in an unordered_set which owns no memory whatsoever. What it will do, is reduce the memory consumption down to the small initial memory consumption of a default-constructed unordered_set, which should be significantly less than the memory consumption of an unordered_set containing a lot of elements.
    – Mankarse
    Nov 12, 2011 at 4:01
2

unordered_set automatically releases the memory it takes if its on the stack:

void some_function()
{
    unordered_set<int> set;
    // use the set
} // set memory will be deallocated automatically here
0

The set is a node-based container, so the memory for the nodes is indeed freed up properly. But since the hash table may have grown to many buckets, you could try rehash(1) to make it smaller again -- but that's only a hint; the set isn't required to reduce the number of buckets.

1
  • I need to free the unordered_set hold by local function. Each time I called local function, it will consume some memory, so I need to free it at the end of local function.
    – weeo
    Nov 12, 2011 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.