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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With