views:

55

answers:

1

I am using tracelistener in a multithreaded application to log message remotely, but the appllication creates memory overflow.

For testing I created 10,000 threads, and tried to log messages using TraceData function.

Does .Net framework create an object for every call to TraceData, which result in memory overflow?

+4  A: 

10,000 threads: each will have a (default) 1MB stack space allocated. Therefore they will need 10GB RAM, which is impossible on a 32bit process (and likely to break total available RAM/Page on 64bit).

Nothing to do with tracing.

Additional: Great new article on thread (and process) limits on Windows, by Mark Russinovich. Please note the final paragraph. "Pushing the Limits of Windows: Processes and Threads"

Richard
what is the max number of threads that we can create ?
somaraj
(Available RAM / 1048576), so 2048/3072 for 2/3 GB of available RAM.
schnaader
Depends how much address space you need for other things, each thread blocks other allocations, but the real question is how many so that they are doing useful things. I would not expect to hit 100 threads (on even the biggest current servers) today. (As more cores and memory supported, expect to see that grow slowly.)
Richard
HI Richard ,Thanks for the answers . I also have requirement to logg the message locally , where i am using appendalltext function . the same I tested with the 10,000 threads . But didnt created any memmory overflow issue .if 1MB stack space for each thread was the problem , the same should happen this time also , correct ..
somaraj
@somarajL Each thread will allocate 1MB of address space. Are you sure you were creating that many threads? I would expect 10% that many to almost stop your machine (too much work for the kernel scheduler). But without seeing the test code can't be specific.
Richard
Code look like thisstring message = "Hellow World";static void log(){LogEntrySource.TraceData(this.LogEntryEventType, this.LogEntryLogLevels, Message);//log locally// WriteMessageLocally( message)}Thread[] th = new Thread[10000];for(i=0;i<10000;i++){th[i] = new Thread(new ThreadStart(log));th.start();}*************************//Receiving side//even i dont have any implimentation in this public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) {//all code commented }
somaraj
@somaraj: looking at that code, I suspect many of the threads are completing and closing before you have created many. Check out the thread count column in Process Explorer or Task Manager. Add a 10s sleep into the log function and see what happens.
Richard
I tried and it seems 10 millisecond delay has reduce the memmory usage. Why is it said that some thread are completed before they are created . and How does this increase the memmory ? by introducing delay are'nt we introducing seialization .
somaraj
10s == 10 seconds. So the threads don't exit quickly and see the effect of having hundreds of threads running. The log function is so short a thread will be spun up, complete and shut down (freeing its stack) before all the threads have been completed.
Richard
Thanks for all the answers
somaraj