CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    2

    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!

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Failing Garbage Collector at SerialPort object?

    Does your GC have a Collect method (I'm not familiar with micro .NET)? The collect method is a firmer way of calling the garbage collector, but I do know that you can't always perfectly time when collection occurs.

    Do you have to use labels or can you use a true loop? Using an actual loop might make the CLR happier and allow GC to happen sooner. The label hopping could be keeping the CLR from collecting because it's waiting for a break in the code.

    This is all just an educated guess on my part.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Feb 2012
    Posts
    2

    Re: Failing Garbage Collector at SerialPort object?

    Hi CreganTur, Thank you for reply.

    Unfortunately there is no Collect(); method function in .NET MF.
    The reason I use labels by default, because it is a comparement less for the CPU with respect to a while(True); loop. Because labels translate to the instruction to JMP <address> unconditionally in assembly code.
    Atleast, in C++ it does when I view the compiled code in assembly language. If this is also true on a Microcontroller compiled NET MF application I actually do not know.
    I will try it with a while loop and put the actual code in a function and update my findings here.

    Cheers

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Failing Garbage Collector at SerialPort object?

    The Serial Port is an un-managed shared object. So you have to derive an object from IDisposable. to do this correctly research IDisposable, the dispose method, and the using keyword.
    Note: Shared unmanaged resources will be closed the void dispose(bool disposing) method.
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured