Is there a memory leak using .net SerialPort, is there a better way?

I have a program that is used to communicate with hardware through rs232. This software is used to display the data stream that is pushing the rs232 from the hardware as fast as it can be. The problem that I am facing is that over time, the private memory assigned to the program explodes and the program crashes very quickly. If I disconnect the equipment from sending data for about 2 minutes, then the software can clear the memory, but only if I paused the data stream.

I am using the DataReceived event from SerialPort and it seems to be a problem because it will cause a memory burst even if the DataReceived function does nothing inside. The only thing I can think of is that every time this event occurs, it creates a new thread to start, and it happens so fast that the computer does not have time to start the GC during data entry.

Is there a more efficient way to output data from a SerialPort object? Do I only care about the line when I get "NewLine"?

Thank,

John vickers

+3
source share
4 answers

# , , , , .

?

state Init
  recv();
  $len = length($DATA_PACKET);
  if("$len > 0") {
    log($DATA_PACKET, Debug);
  }
end state

- , , .

0

DataReceived . , . - :

while (this.serialPort.IsOpen)
{
    int b = this.serialPort.ReadByte();
    if (b != -1)
    {
        // data is good here
    }
}

, , - , .

+4

, . SerialPort threadpool DataReceived. , TP . , .

, , , , , . Handshake, TP, .

Debug + Windows + Threads. , , . DataReceived, , , . , , , .

, DataReceived , , Control.Invoke(). , . Handshake . , . ErrorReceived btw, , , .

TP, . , 250 . .

+4

.

DataReceived.

USB 3G-, . , DataReceived. - .

, 10 . .

Read (...) . , , , .

0

Source: https://habr.com/ru/post/1776547/


All Articles