0

I read data from serialport in byte[] then show in hex string in richtexbox.

private string ByteArray_to_Hex(byte[] data) {
    StringBuilder sb = new StringBuilder(data.Length * 3);

    foreach (byte b in data) {
        buffer_log.Add(b); // save data in buffer to write to file
        sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
    }

    GC.Collect();
    return sb.ToString().ToUpper();
}

private void Log(string msg) {
    try {
        Richtexbox.Invoke(new EventHandler(delegate {
            Richtexbox.AppendText(msg);
            if (Richtexbox.Text.Length > 1000000) {

                // seem to slow when comparing to amount of data reading
                Richtexbox.Select(10000, 950000); 
                Richtexbox.SelectedText = "";
            }
        }));
    } catch {
    }
}

private void PLay_Click()
{
    Log(ByteArray_to_Hex(data_come_from_serialport);
}

It consume lots of memory since there are lots of texts. But when I call

private void menu_Clear_Click(object sender, EventArgs e) {
    Richtexbox.Clear();
    buffer_log.Clear();
    GC.Collect();
}

It isn't release memory, still consume lots of memory in ram. What 's the best way to release these memory?

Update:

since it's the best way. What is this problem. I describe my problem I had: This app always has systemoutofmemory exception when runing for 1 hour. If I run this app for 55 minutes then call Clear method, it only work for 5 minutes and has systemoutofmemory exception. Can someone explain this ?

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!SerialPort.IsOpen)
            {
                return;
            }
            if (flag_stop)
            {
                return; 
            }
            int bytes = SerialPort.BytesToRead;
            byte[] buffer = new byte[bytes];
            SerialPort.Read(buffer, 0, bytes);
            Log(ByteArray_to_Hex(buffer));
        }   

update way to read data from serialport.

Community
  • 1
  • 1
  • 3
    Do nothing. That's the best way in almost every case. Your case is not an exception. Almost guaranteed, you are misunderstanding whatever information is telling you it's using "lots" of memory. – Andrew Barber May 16 '14 at 05:39
  • I see memory used by this app in task manager before and after it run (for an hour then call Clear method). – user3640235 May 16 '14 at 05:42
  • 1
    Task Manager is a bad way to measure memory usage. Among other things, it is showing _virtual_ memory, not physical memory. – John Saunders May 16 '14 at 05:43
  • 2
    You lack a basic understanding of the environment you're working in: it is _managed_. You don't manage the memory .. the environment does. – Simon Whitehead May 16 '14 at 05:43
  • since it's the best way. What is this problem. I describe my problem I had: – user3640235 May 16 '14 at 05:53
  • Why do you think it is a problem? – phoog May 16 '14 at 05:56
  • What are you trying to achieve? Maybe you're going about it the wrong way? Also, why did you give us the `Log` method, it is never called. If you're more specific of what you want to do it is easier to help, and maybe find another solution to the problem. – Tomas Jansson May 16 '14 at 05:57
  • Are you calling any unmanaged code at some point? How are you getting the serial port data? – Matthew Watson May 16 '14 at 06:26
  • Just call SerialPort.Open() in another button @MatthewWatson – user3640235 May 16 '14 at 06:31

1 Answers1

2

You should not need to do anything. Thats the magic of managed code.

But you want to try to trigger the garbage collector you can use.

    GC.Collect();
    GC.WaitForPendingFinalizers();

But read this first and then this and finally the dry boring msdn text

Community
  • 1
  • 1
Archlight
  • 2,019
  • 2
  • 21
  • 34
  • probably, better GC.Collect(2); to run garbage collection for all generations – Dmitry Bychenko May 16 '14 at 06:12
  • useful but can't find a way to solve my problem here. – user3640235 May 16 '14 at 06:36
  • I didn't see your edit unitll now. If your machine is chugging memory like that the GC WILL get triggered. But the GC has decided that it can't relase the memory... that means you must have references some buffers or not cleaning up propertly. Try to comment out everything. and then reintriduce functions one by one. – Archlight May 16 '14 at 13:28