Dig the source code using push_back:
it checks if there is enough space for the new element, if not, it redistributes the matrix with space for (current_size * 3 + 1) / 2 ( see here ). In your example about 400 000 * 256 (total 102 400 000 elements) it tries to perform a different distribution, so it attempts to allocate space for 001/2 = 307 200 153 600 000 elements. But in order to move this, he needs to allocate a new space, and then copy the data
From matrix.cpp :
Mat m(dims, size.p, type());
size.p[0] = r;
if( r > 0 )
{
Mat mpart = m.rowRange(0, r);
copyTo(mpart);
}
*this = m;
So this is essential:
- Selects a new matrix using the default constructor for all newly created elements.
- Copy data and delete old data
- ( )
, (600 000 + 400 000) * 256 - 1 4 . 600 000 , 2400 000 .
, , 600 000 , 900 000x256 (900Mb) + 600 000x256 (600Mb) + 600 000 (~ 3.4Mb). , ( push_back), .
: , reserve . ( ).
, , , , .
: realloc malloc/memcpy?