3

I have a uBLAS matrix, like so:

boost::numeric::ublas::matrix<double> mat(50000,50000);

Once I'm done with a set of calculations on the matrix, I want its memory freed.

I have been using mat.clear() which, according to the docs, "clears the matrix". But my program keeps running out of memory.

Digging into the headers, I find this:

void clear () {
  std::fill (data ().begin (), data ().end (), value_type/*zero*/());
}

So there's a clear semantics problem with clear().

The question then is, how do I ensure that the memory is freed?

Richard
  • 56,349
  • 34
  • 180
  • 251
  • Do you happen to have found a solution to the problem? – Thomas Mar 14 '15 at 06:43
  • Did you find a solution to this problem? I seem to be running into he same issue. An in-elegant way of handling the issue would be resizing the matrix to (0,0). I was wondering if there is a "right" way to do it. – Devil Aug 26 '15 at 16:19
  • @Devil, but is resizing guaranteed to release memory? For instance, with `std::vector` a resize does not make such a guarantee since the vector may be enlarged again later and allocations are expensive. Instead, the C++11 `shrink_to_fit()` function must be used. I never did find an adequate solution to this problem and eventually switched to using an std::vector of std::vectors since I mostly wanted uBLAS as a handy 2d matrix. – Richard Aug 27 '15 at 06:28

1 Answers1

0

An inelegant way to return the memory is by resizing the matrix:

mat.resize(0,0,false);  

Though I have not tried using zero for a size value myself...

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105