1

The code below is my entire test program. Each time I press ENTER, the RAM that the process is using is increasing with 4k (it will keep increasing, without stopping; I am seeing it with task manager). What is wrong? The same things happens with _beginthread.

I am trying to write a server, and I want to process each connection with a thread. (Note that this means that I can't join the thread, because that will pause the main thread from accepting new connections.)

unsigned __stdcall thread_test(void *)
{
  for(int i = 0; i < 10000; i++)
  {
      i+=1;
      i-=1;
  } //simulating processing

  _endthreadex( 0 );
}

int main()
{
  HANDLE hThread;
  while(1)
  {
    getchar();

    hThread = (HANDLE)_beginthreadex( NULL, 0, thread_test, 0, 0, NULL );
    CloseHandle( hThread );
  }
}

Compiled with code blocks and visual studio.

EDIT: I've made some tests, and the memory stops filling up once it reach around 133.000K (when the program starts, the memory is around 800k); but at this stage, the program runs like 4-5 times slower than it did in the beginning (higher the memory - slower the program runs), so it would not be good for my server to run like that.

EDIT 2: I've got Visual Studio 2013 and the problem gone.

EDIT 3: If I test the code above in Visual Studio 2013, it gives no leaks. But if I use beginthreadex with a small server code, it gives me leaks like before, each request giving 4k. Here is the server testcode(it does nothing, only to see that it leaks memory) that I use http://pastebin.com/EDmJXkZU . You can compile it and test it by typing your IP into the adress bar of the browser.

  • Have a look at the number of threads in that process while it runs. – danielschemmel May 10 '14 at 17:53
  • it shows 1 thread (yes, in the thread collumn) – user3565078 May 10 '14 at 17:54
  • _'So I can't join the thread, because that will pause the main thread from accepting new connections.'_ You may provide a callback, to signal from the thread when it ended, and you can join it in your main loop. Also you should consider rather using the [standard thread idioms](http://en.cppreference.com/w/cpp/thread). – πάντα ῥεῖ May 10 '14 at 17:58
  • I really want to use _beginthreadex and not the thread class. – user3565078 May 10 '14 at 18:03
  • FWIW, you can't “join” a thread in Windows anyway. That’s a pthreads concept that has no meaning in Win32; the equivalent would be waiting on the handle and then closing it, but you've closed it already (the equivalent of “detach” in pthreads terminology). – al45tair May 10 '14 at 18:11
  • Also, you don’t need to call _exitthreadex() in the above code. It gets called automatically when your entry point returns. As for the 4KB, I'd guess it’s the thread’s stack; you could probably test this theory by passing 8192 instead of 0 for the stack_size argument and seeing if that changes things. Also, it may not be a “real” leak; it rather depends on how it’s allocated and what exactly is happening when the thread terminates. – al45tair May 10 '14 at 18:14
  • The class thread has join method, and it is windows. Forgot about the join part...what I wanted to say there is that i don't want to close something later from the function main. I want my server to works like one of the hundreds of multithreading examples that are on the internet, but aparently, it doesn't. – user3565078 May 10 '14 at 18:17
  • i see in task manger that the increasing memory won't stop...so I think it is real. I just tested that theory, but it fails, because it's still taking 4k. – user3565078 May 10 '14 at 18:21
  • which compiler do you use ? I can't reproduce your problem (VS2013). – engf-010 May 10 '14 at 20:45
  • Here is a link to a possibly related problem : http://social.msdn.microsoft.com/Forums/vstudio/en-US/620df5f3-37a4-439d-b7cd-c5691eaca01d/memory-leak-caused-by-beginthread-beginthreadex?forum=vcgeneral – engf-010 May 10 '14 at 20:46
  • I am using visual studio 2010, but from your link you can see that the 2013 version does not have that problem. Strange is the fact that this hapens in codeblocks too. – user3565078 May 10 '14 at 20:51
  • Yes ,the link doesn't really provide an answer. Maybe you can download VS2013 Express and try it with that version. It'll give at least a fast 'answer' – engf-010 May 10 '14 at 20:53
  • I can't reproduce the problem using VS2010. Perhaps it's something to do with CodeBlocks, or there might be other variables. Which memory column are you looking at with Task Manager? How can you tell that the program is running more slowly, how are you measuring this? – Harry Johnston May 11 '14 at 21:35
  • Also, I note that your program doesn't even compile as shown, because the `thread_test` function doesn't provide a return value. Can you please post the actual code that you're testing with? – Harry Johnston May 11 '14 at 21:49
  • that is the entire code as you see above...except the headers. It compiles with no errors in codeblocks. – user3565078 May 12 '14 at 03:33
  • OK, so since nobody seems to be able to reproduce this with just Visual Studio, my bet is that it's a codeblocks issue - perhaps something to do with the debugger. Question retagged accordingly. – Harry Johnston May 12 '14 at 21:19
  • It's happening in visual studio too. – user3565078 May 13 '14 at 18:12
  • I've tested your supplied code (VS2013 & Win 7 Ultimate x64) and modified it with starting each while loop 150 threads saving the handles in a vector and closing all handles (every while loop). Memory consumption before the while-loop is ca. 920 KB and while runnning it goes up to about 1200 KB and dropping again to about 980 Kb (over and over again) for about 30.000 while loops. So at my machine, it seems Ok. – engf-010 May 14 '14 at 00:30
  • Have you been able to reproduce the problem on more than one machine? It might be due to a virus, malfunctioning software, or faulty device drivers. – Harry Johnston May 18 '14 at 23:11

1 Answers1

0

Task Manager is not showing RAM used by your program. For a better view use Task Manager's Resource Monitor and observe the private bytes indication. But all memory monitors show only "virtual memory," which is commonly retained by the runtime library instead of being freed back to Windows. You don't have a real problem.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • Actually I have a problem because if the memory that task manager shows is high, then the program runs much more slower (wich I don't want, because it's not a one time run program, it's a server) – user3565078 May 10 '14 at 21:02
  • @user3565078 what do you mean? You can *see* it run slower, or you *think* high memory usage will make it run slower? – jalf May 15 '14 at 19:13