In my project I use COM2. I connect it with the pc via a TTL-232R-3V3 cable.
Before any message is sent, the GC says:
GC: 1msec 19752 bytes used, 44628 bytes available
But every time I send bytes, it consumes a little memory.
after about 40 messages there is not enoufh memory to allocate.
GC: 8347 bytes available.
The Port doesn’t receive any data from then.
I am sure the memory leak is not in the other code, because, I have another thread, sleeping and sometimes(every sec) looking if the variable "geheugenVol"is true.
then the serial port is disposed, and assign new again. The memory is back at 44kB.
all the consumed memory is in:
Type 1E (BINARY_BLOB_HEAD )
Could someone please tell me how do clean my memory proper, instead of disposing my port?
I use the code below, but I deleted the code between the cases 5, 6 and 7.
static byte[] buffer1 = new byte[1];
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buffer2, buffer3;
if (port != null)
{
if (!geheugenVol)
{
int aantal = port.Read(buffer1, 0, 1);
switch (buffer1[0])
{
case 1://enkele bitstream
buffer2 = new byte[44];
buffer2[0] = buffer1[0];
port.Read(buffer2, 1, 43);
port.Write(new byte[] { 10 }, 0, 1);
if (!BEzig)//is set in "AfstandBediening"
{
AfstandBediening(buffer2);
port.Write(new byte[] { 12 }, 0, 1);
}
break;
case 3:
buffer2 = new byte[2];
port.Read(buffer2, 0, buffer2.Length);
int grootte = (buffer2[0] * 255) + buffer2[1];
buffer3 = new byte[grootte + 2];
port.Read(buffer3, 2, buffer3.Length - 2);
buffer3[0] = buffer2[0];
buffer3[1] = buffer2[1];
VoegTijdschemaToe(buffer3);
buffer3 = null;
buffer2 = null;
Debug.GC(true);
port.Write(new byte[] { 15 }, 0, 1);
break;
case 5://verwijder tijdschem
buffer2 = new byte[2];
port.Read(buffer2, 0, buffer2.Length);
if (buffer2[0] == buffer2[1])
{
port.Write(new byte[] { 10 }, 0, 1);
VerwijderTijschemas();
port.Write(new byte[] { 14 }, 0, 1);
}
break;
case 6://zet correcte tijd
buffer2 = new byte[6];
port.Read(buffer2, 0, buffer2.Length);
DateTime time = new DateTime(((int)buffer2[2] + 2000), (int)buffer2[1], (int)buffer2[0], (int)buffer2[3], (int)buffer2[4], (int)buffer2[5]);
Utility.SetLocalTime(time);
port.Write(new byte[] { 16 }, 0, 1);
break;
case 7://ping
port.Write(new byte[] { 9 }, 0, 1);//ping back
BEzig = false;
break;
default:
port.Write(new byte[] { 17 }, 0, 1);
break;
}
buffer2 = null;
buffer3 = null;
port.Flush();
port.DiscardInBuffer();
port.DiscardOutBuffer();
Debug.GC(false);
}
uint a = Debug.GC(true);
if (a < 20000)
{
geheugenVol = true;
}
}
}