I have C# .net application which reads data from the serial port and launches a new thread to process the data when all data has been read (end of data is determined by timing constraints).

I create a new object where the thread will run, copy the data that I just read from the serial port, and call the run function to launch the thread, in which I return immediatly (for testing purposes), so the thread should die. Everytime I launch a new thread the memory increases significantly. And the memory never goes down. After running the application for 20mins, and launching perhaps 600 threads in total (none of which should have ever ran concurrently), the memory consumption of the application explodes to beyond a gigabyte.

Below is a part of thread-launching code:

Thread workerThread;
SerialPortThread newthread = new SerialPortThread(this, m_PaymentCore, comport, Data,BytesRead);

workerThread = new Thread(newthread.ProcessData);

The first 3 parameters going into constructor are objects, whose references I reuse. The 4th is a byte[] which I copy into a byte[] inside of the newthread object, using a for-loop. You can be assured that after starting the thread, it exits immediately, so the allocated data should be released. However, even if it did not, it would not explain why my memory baloons to 1 gigabyte when I am allocating, through this time in all threads, at most 5k-10k.

I also tried thread pooling, and it gave the same result.

What is the deal here?

changing the property IsBackground = true; should help ya. however, if this is not working, add an event handler and when the work is complete, you can simple do Thread.CurrentThread.Abort() when an event is raised.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.