Home > Software design >  Change value of a string multiple times does affect memory?
Change value of a string multiple times does affect memory?

Time:06-27

I am using a serial device which send always data. So each time data is been received I save them into a string.

private static string TemporaryData = "";

private static void DataReceivedHandler(
                    object sender,
                    SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string indata = sp.ReadExisting();
    TemporaryData = indata;
}

Can this code affect my memory? And leading to memory leak?

CodePudding user response:

Since the Length property of System.String is an Int32, the maximum length would be 2,147,483,647 chars (max Int32 size). But note that this is the theoretical limit and the practical limit is nowhere near that. So anything longer than that may throw an exception.

  •  Tags:  
  • c#
  • Related