OutOfMemory Exception in serial Port RRS feed

  • Question

  • Hi,

    I have an Application which needs to poll serialports continously, to detect some devices. I need to try with diffrent baudrates and hence i need to close a port before opening it with another baudarate.
    Also i need to close the ports in my polling thread since same port i need to use in another thread which is used to download data from the already detected port.

    My problem is, sometimes  I am getting OutOfMemory Exception in serialport.Open method.
    I am doing the following thing in my serialPort class

    1. Whenever I Close a port I keep my thread sleep for 200 milliseconds so that it closes properly.

    I checked for any memory leaks using the port monitor

    I didn't see any dramatic increase in the following counters

    process/No of private bytes
    .CLRMemory -Bytes in all Heaps
    .Net CLR Locks and threads - no of current logical threads

    Can anyone help me in this regard.


    Monday, December 22, 2008 5:53 AM

All replies

  • Hi,

    Yes I do also!

    You get out of memory an all .Net versions (2.0,3.5, 3.5 with pack1, 4)

    More so in windows 7 with 64bit and under VS2010.

    I can see the problem and the fix is not a simple one......


    The problem is with the DataRecived Event in the SerialPort Class. The Event will Fire faster than the users event handler can handle the data.

    Each new event is poor at handling the memory and some times does not GC the memory buffer after itself.

    I am so feed up with this lack of basic functionality with Microsoft!

    I have written a new one for now in assembler and called it via a DLL what a pain!.


    I think Microsoft plays with the big code (WPF, Cloud and other own goals)  and has dropped the ball, We all welcome Google and Apple.


    Regards a very tired VS developer..........

    Monday, November 14, 2011 12:57 PM