-1

I have a std::vector of structure and a cv::Mat in each structure. I do vec.erase(iter) to remove specific element from vector. But std::vector does not release memory indeed. As I check GPU memory it reaches to 8GB while vec has a few element.

Also I utilized boost::container::vector to creating vec but also memory did not release. How could I release memory while vector really has a few members.

I'm worrying about memory leaks.

I also did vec.shrink_to_fit() but it was not useful.

BarzanHayati
  • 637
  • 2
  • 9
  • 22

1 Answers1

0

vector.erase() only invalidates iterators and references from the erased element onward (as following elements are moved forward).

Shrinking the vector (a contiguous memory container) would require allocating a new, smaller memory block, and copying over all vector contents. This would invalidate all iterators and references.

Your memory is not leaked. The vector just retains the memory (capacity > size).

shrink_to_fit() is non-binding. It is a request for the implementation to release unused memory at the end of the vector. The implementation may legally ignore that request.

You could do the "copy over to a new vector of appropriate size" manually.

Or, depending on your use case, using a forward_list, list, or deque might be a better, more memory-efficient fit.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Does linked list or `forward_list` has a better performance than `std::vector`? Does C++ retains memory in this case, also? – BarzanHayati Sep 18 '22 at 11:53
  • @BarzanHayati "Better performance" is relative. They use one (`forward_list`) resp. two (`list`) additional pointers per element. Neither allows random access. `forward_list` can only be iterated forward, `list` also backward. But erasing an element is O(1), and the memory for the element can immediately be released. It's a trade-off, as everything. – DevSolar Sep 18 '22 at 13:02
  • Random access granted by language not by OS, am I right? If language does that why C++ does not release memory as an extra option? – BarzanHayati Sep 18 '22 at 15:15
  • @BarzanHayati Random access has nothing to do with the OS. "Extra options" can make a class unwieldly, and `vector` is supposed to be the workhorse of C++. Your use-case -- frequent `erase()`, itself a rather expensive operation -- is uncommon. Perhaps have a look at Boost.Container's [stable_vector](https://www.boost.org/doc/libs/release/doc/html/container/non_standard_containers.html#container.non_standard_containers.stable_vector). – DevSolar Sep 18 '22 at 15:28
  • Thanks: Yes I do a lot of `erase()` and `push_back()`. – BarzanHayati Sep 18 '22 at 15:42
  • But -- if I may ask -- why are you doing this in one massive vector? I can't help but think that your problem is not so much "which container to use", but "why is all this *in one container* in the first place?" – DevSolar Sep 18 '22 at 16:14
  • A vector of structure to hold enough information of tracked faces. So I must add a new persons and remove disappeared ones**(Too many adds and removes)**. As **std::vector** does not release memory(here **GPU memory**), I'm looking for a suitable alternative container. I'm not sure which one is better: `std::vector` , `std::forward_list`, `boost::container::stable_vector` or `boost::container::vector`. This alternative should be fast enough, because I do a lot of searches by `std::find_if` or `boost::container::find_if` – BarzanHayati Sep 18 '22 at 16:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248156/discussion-between-devsolar-and-barzanhayati). – DevSolar Sep 18 '22 at 16:23