|
-
February 16th, 2012, 04:07 PM
#1
Failing Garbage Collector at SerialPort object?
Hi all,
I program for a living. But it is still my hobby. I am trying to code a microcontroller (Fez Panda II) with the Micro subset of .NET. Everything is going quiete well. Except that I run into an OutOfMemoryException. The GC reports that less and less memory is available after each iteration of the following loop:
Code:
Begin:
SerialPort test = new SerialPort("COM1");
test.DataReceived += new SerialDataReceivedEventHandler(test_DataReceived);
test.Open();
string StrTest = "klaas";
test.Write(_encoder.GetBytes(StrTest), 0, StrTest.Length);
test.Flush();
test.Close();
test.DataReceived -= new SerialDataReceivedEventHandler(test_DataReceived);
test.Dispose();
test = null;
Microsoft.SPOT.Debug.GC(true);
goto Begin;
For as far as I know, I clean up the SerialPort object properly.
This code on the other hand seems to run fine, no increase in memory usage:
Code:
SerialPort test = new SerialPort("COM1");
test.DataReceived += new SerialDataReceivedEventHandler(test_DataReceived);
test.Open();
string StrTest = "klaas";
ResendData:
test.Write(_encoder.GetBytes(StrTest), 0, StrTest.Length);
test.Flush();
Microsoft.SPOT.Debug.GC(true);
goto ResendData;
test.Close();
test.DataReceived -= new SerialDataReceivedEventHandler(test_DataReceived);
test.Dispose();
test = null;
Now disposing and re-creating SerialPort objects is ofcourse by no means usefull. I can not imagine a scenario I would want to do this. But, the memory is not properly cleaned up.
In a larger application on the microcontroller I experience an increment in memory usage, while I do NOT dispose and re-create the SerialPort object. And, remarkbly, I even get exceptions en routines that send data when I clean up the SerialPort. As my application is synchrous the send functions are not triggered during the disposal of the SerialPort object.
The only way around is resetting the microcontroller (programmatically), but re-reading all the settings at startup is killing my low-power consuming goal.
Could anyone please shed some light on this.
Thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|