Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSVC Heap Reserve/Commit

I wrote a program that requires ~1GB of RAM zeroed at startup, and this is taking more time than the rest of the program combined from a cold start. (it's a simple program, subsequent runs take almost no time at all to allocate). When watched via task manager, it starts with almost no RAM, and then grows about 2MB/sec until it reaches the 1GB it needs. I found options "Heap Reserve Size" and "Heap Commit Size", and set them to 1000000000 each, but when I watched the program via task manager, the program appears to start at 1GB, and then grow at that 2MB/sec until I get a bad_alloc. The line in question is: std::vector<std::vector<char> > data(512, std::vector<byte>(2097252, 0));
Can someone clearly explain what these options are for, and if there is a way to use them to speed my allocations? I have read http://msdn.microsoft.com/en-us/library/f90ybzkh(v=vs.80).aspx, but it doesn't explain the behavior I observed.

like image 757
Mooing Duck Avatar asked May 01 '26 17:05

Mooing Duck


1 Answers

Your memory allocation is not causing your speed issues. Your speed issues are being caused by copying ~2MB of zeros 512 times. Changing those properties isn't going to help you.

I would investigate efficient ways of zeroing out memory, rather than relying on std::vector to do it for you. Or, at the very least, don't pass a std::vector of 2MB of data as an argument to the constructor. Let the default constructor create 512 empty vectors, then resize each of them to 2MB. At least that won't be a memory copy; just a bunch of memory writes.

like image 63
Nicol Bolas Avatar answered May 03 '26 07:05

Nicol Bolas