3

I'm messing around with some threads in preparation for some server work I will do. I'm checking for memory leaks with some simple looping code. I noticed that running some code caused a memory leak. The code I assumed was leak proof as it was very simple. So I tried again but this time without any code, just a empty function and I'm still getting the same results.

#include <thread>
#include <windows.h>


void doNothing() {}

int main() {
    for (int i = 0; i < 10000; i++) {
        std::thread thread_obj(doNothing);
        thread_obj.join();
    }
    Sleep(10000000); // <- this sleep here is just so I can monitor the memory in Visual Studio 2019
    return 0;
}

Here's the memory usage graph. The point where it flattens out is when it hits the sleep

enter image description here

Heap size after hitting the sleep

enter image description here

And here's the Stack view. Here I'm getting some puzzling results.

enter image description here

Can anyone explain what I'm doing wrong?

I am using x64, Visual Studio 2019 16.5.2

If the answer is obvious, or I'm doing something stupid - I apologise in advance, as this issue seems so fundamental I'm 99% sure I'm doing something wrong.

Tom
  • 1,235
  • 9
  • 22
  • can you specify which version of msvc you are using and whether x86 or x64. – Thomas May 10 '20 at 17:42
  • @Thomas I am using x64 and MSVC 142 – Tom May 10 '20 at 17:57
  • MSVS should be something like 2019 (16.x) or 2017 (15.x) 142 is the Platform Toolset. – Thomas May 10 '20 at 18:02
  • cannot reproduce. stays at 3MB for me, both x64 and x86 with VS 2019. – Thomas May 10 '20 at 18:13
  • I was running it in debug x84. Running it in both debug x64, release x84 and x64 both results in increasing amount of memory depending on how many iterations the loop is given – Tom May 10 '20 at 18:17
  • I cannot reproduce with VS2019 16.5.4 and x64, tested debug and release. I would check for handle leaks with https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer and for memory leaks try https://stackoverflow.com/questions/4790564/finding-memory-leaks-in-a-c-application-with-visual-studio. – Werner Henze May 10 '20 at 18:17
  • I would first update VS, then test again. And make sure that you are using the latest toolset. – Werner Henze May 10 '20 at 18:19
  • There's many different kinds of memory statistic. I don't know what "Process Memory" means either to Windows or to your toolchain, but 6Mb is not huge. How long did your test take to create and destroy 10,000 threads? How do you know that the "Process Memory" wouldn't flatten out at some reasonable level if you did 100k threads, or a million threads? When you "free" heap-allocated objects in most programming languages, the library gives the freed blocks back to the heap, but it practically _never_ gives allocated virtual memory back to the operating system. – Solomon Slow May 10 '20 at 19:12
  • P.S., If your "server work" is supposed to be high-performing, then you may be off to a bad start if it needs to create and destroy so many threads. Consider using a _thread pool_ instead. – Solomon Slow May 10 '20 at 19:13
  • @SolomonSlow Thanks for the feedback! I was unaware that the creating of threads has such a impact on OS memory, I was under the assumption that the code within the threads were the memory intensive part. I'm using the http server library https://github.com/yhirose/cpp-httplib which has thread pooling support, so I suppose this question is more out of curiosity at this point. I was more alarmed at the fact the memory size was increasing rather then the actual size of it. – Tom May 11 '20 at 11:15

0 Answers0