im my project the memmory usage begines with 9200k and goes to 150000Mb!!
why?
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;using
System.IO.Ports;
using
System.Text.RegularExpressions;
namespace
SimpleSerial{
public partial class Form1 : Form{
// Add this variable string RxString; public string displine1; public string displine2; public Form1(){
InitializeComponent();
serialPort1.PortName =
"COM5";serialPort1.BaudRate = 19200;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; //eksafanizei ta pano button tis formas //this.TopMost = true; //me true always on top i forma //WindowState = FormWindowState.Maximized; //i forma na einai form maximize //orizoume syntetagmenes tou position int monitorHeight; int monitorWidth; int monitorcenterHeight; int monitorcenterWidth; int formposition;monitorHeight =
SystemInformation.PrimaryMonitorSize.Height; //pernoume stin metavliti monitorHeight to Height tis trexousas analisis tis othonismonitorWidth =
SystemInformation.PrimaryMonitorSize.Width; //pernoume stin metavliti monitorWidth to Width tis trexousas analisis tis othonismonitorcenterHeight = monitorHeight / 2;
// to dieroume /2 gia na broume to kentromonitorcenterWidth = monitorWidth / 2;
formposition = monitorWidth - 390;
this.StartPosition = FormStartPosition.Manual; //energopioume manual position this.Location = new Point(formposition, 0);textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
if (!CheckIfPortExists(serialPort1.PortName))
{
MessageBox.Show(serialPort1.PortName + " doesn't exist on this computer");Close();
}
else{
try{
serialPort1.Open();
}
catch (Exception ex){
MessageBox.Show(ex.Message); //MessageBox.Show("Serial port in use");}
}
}
private bool CheckIfPortExists(string portName)
{
foreach (string name in SerialPort.GetPortNames()){
if (name == portName) return true;}
return false;}
private void DisplayText(object sender, EventArgs e){
foreach (string strItem in Regex.Split(RxString, "#2#")){
displine1 = strItem;
}
textBox1.Text = displine1;
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
System.
GC.Collect(); //meionei tin mnimi kai kanei suspent to threadSystem.
GC.WaitForPendingFinalizers(); ////RxString = serialPort1.ReadExisting();RxString = serialPort1.ReadLine();
//meionei tin mnimi kai kanei suspent to thread this.Invoke(new EventHandler(DisplayText));}
}
}
Ryan AlfordPosted Oct 25, 2008, 12:41 PM
nvraman19Posted Oct 25, 2008, 12:38 PM
I couldn't see the definition for the serialport1 object. In general, if you see the memory been increased, it is because the resources are not properly closed and released. Check for all the objects that you are using and release it explicitly. From your code, the serialport1 object has not been released, and it has reference to the ports, so in each call, the memory is been cramped. So my suggestion would be to
1. Look in for the objects which uses expensive resources such as network call and others.
2. Try to release it as soon as it purpose is over and try to reduce the scope for those objects (least scope is highly reccommended)
3. If the performance is not the constraint, then follow the code as below:
className c1;
try
{
c1 = new className();
c1.CallMethodWithResource()
}
finally
{
c1 = null; //release it
GC.Collect(); //In general GC.Collect() should be used sparingly and allow the dotnet to handle memory release
GC.WaitForPendingFinalizers();
}