我有一个与嵌入式控制器(Modbus)通信的程序(C#),接收数据并将其记录到数据库。此外,它还在用户界面中显示数据。
每5或8天程序崩溃(在每台计算机上)。
大部分时间没有例外,只有Windows默认消息“'程序名'已停止工作”。
每隔一段时间我就会得到'System.OutOfMemoryException:内存不足'。例外。 我认为这个程序设计得不好,因为内存使用量一直在增加。
一周后达到800Mb。
这是问题吗?
我每天都尝试重置程序。它没有解决问题。
这是来源:
namespace AtRegisterData
{
public class RegisterDataManager
{
modbus mb;
public RegisterDataManager()
{
try
{
mb = new modbus();
}
catch (Exception ex)
{
writeError(ex.Message);
}
}
public static bool systemOnOffStatus = false;
public static short systemOnOffState = 0;
public static short reactorNum = 0;
public int openCom(ReactorConfig reactorData)
{
try
{
if (mb.Open(Convert.ToString(ReactorConfigManager.ModBusConfiguration.com_port), Convert.ToInt32(ReactorConfigManager.ModBusConfiguration.ModBusBaudRate), 8, ReactorConfigManager.ModBusConfiguration.parity, ReactorConfigManager.ModBusConfiguration.stopBit))
{
return 1;
}
else
{
return 0; // No connection
}
}
catch (Exception ex)
{
writeError(ex.Message);
return 0;
}
}
public int CheckConnection(ReactorConfig reactorData)
{
try
{
// Needs to take the function data from public struct
if (mb.Open("com20", 115200, 8, Parity.None, StopBits.One))
{
return 1;
}
// Needs to take the function data from public struct
if (mb.Open("com1", 9600, 8, Parity.None, StopBits.One))
{
return 1;
}
else
{
return 0; // No connection
}
}
catch (Exception ex)
{ writeError(ex.Message);
return 0; }
}
public List<ReactorEvent> getEvents(ReactorConfig reactorData)
{
try
{
ushort registerNum;
List<ReactorEvent> eventsList = new List<ReactorEvent>();
// List that will hold all of the data (all the registers)
List<ushort> registerValues;
// Get's the data from the modbus
for (int i = 0; i < ReactorEventConfigManager.ReactorEvents.Count; i++)
{
registerNum = Convert.ToUInt16(ReactorEventConfigManager.ReactorEvents[i].ModBusRegisterNumber); // Convert the register numb to Unsigned short
registerValues = new List<ushort>(); // Initlize the list
try
{
mb.SendFc3(reactorData.ModBusAddress, registerNum, 1, registerValues);
}
catch
{
throw new NoConnectionException();
i = ReactorEventConfigManager.ReactorEvents.Count;
}
if (registerValues.Count > 0 && registerValues[0] == ReactorEventConfigManager.ReactorEvents[i].onFlag) // If the event is on?{eventsList.Add(ReactorEventConfigManager.ReactorEvents[i]);}
{
eventsList.Add(ReactorEventConfigManager.ReactorEvents[i]);
}
}
return eventsList;
}
catch (Exception ex)
{ writeError(ex.Message);
return null; }
}
/// <summary>
/// Reads data from the controller
/// </summary>
/// <param name="reactorNumber">the reactor's ndbuumber</param>
/// <returns></returns>
public Dictionary<Register, string> ReadData(ReactorConfig reactorData, List<short> unitedRegisters)
{
try
{
//Register tempRegister = new Register();
Dictionary<Register, string> returnData = new Dictionary<Register, string>();
string strRegisterValue;
int registerLocation = 0;
List<ushort> registerValues = new List<ushort>(); // List that will hold all of the data (all the registers)
List<ushort> registerValuesNew = new List<ushort>();
List<bool> coilsValues = new List<bool>();
ushort startRegister, endRegister, midRegister;
startRegister = endRegister = midRegister = 0;
if (getStartAndEndReg(ref startRegister, ref endRegister, LoggerConfigManager.LoggerConfiguration.RegistersToLog) == 1)
{
try
{
if (endRegister >= 125)
{
midRegister = Convert.ToUInt16(endRegister - 124);
mb.SendFc3(reactorData.ModBusAddress, startRegister, 125, registerValues); // Get's the data from the modbus
mb.SendFc3(reactorData.ModBusAddress, 125, midRegister, registerValuesNew); // Get's the data from the modbus
if ((systemOnOffStatus == true) && (reactorNum == reactorData.Number))
{
shutdownLamp(reactorData, systemOnOffState);
systemOnOffStatus = false;
}
for (int t = 0; t <= registerValuesNew.Count - 1; t++)
{
registerValues.Add(registerValuesNew[t]);
}
}
else
{
mb.SendFc3(reactorData.ModBusAddress, startRegister, endRegister, registerValues); // Get's the data from the modbus
if ((systemOnOffStatus == true) && (reactorNum == reactorData.Number))
{
shutdownLamp(reactorData, systemOnOffState);
systemOnOffStatus = false;
}
}
}
catch (Exception ex)
{
systemOnOffStatus = false;
writeError(ex.Message);
throw new NoConnectionException();
}
// This loop will organize all of the data received from the modbus in a dictionary
for (int i = 0; i <= registerValues.Count - 1; i++)
{
registerLocation = findRegisterLocationInList(startRegister + i, LoggerConfigManager.LoggerConfiguration.RegistersToLog); // Finds the register to copy by the register number
if (registerLocation != -1)
{
Register tempRegister = new Register();
copyRegisterData(tempRegister, LoggerConfigManager.LoggerConfiguration.RegistersToLog[registerLocation]); // Copy the register Info
if (isRegisterInlist(tempRegister.RegisterNumber, unitedRegisters)) // Check if I need to unite to register to 1 4 byte variant
{
strRegisterValue = Convert.ToString(get4Byte(registerValues[i], registerValues[i + 1])); // Unite 2 registers to 4 byte
i += 1; // Need to skip the next register
}
else
{
if (tempRegister.DivideBy != null && tempRegister.DivideBy.HasValue)
strRegisterValue = Convert.ToString((double)registerValues[i] / tempRegister.DivideBy);
else
strRegisterValue = Convert.ToString(registerValues[i]);
}
returnData.Add(tempRegister, strRegisterValue);
}// Insert the data into dictionary
}
return returnData;
}
throw new NotImplementedException();
}
catch (Exception ex)
{
writeError(ex.Message);
return null;
}
}
答案 0 :(得分:5)
使用dotTrace等内存分析器运行程序5-8天。这将为您提供实际消耗记忆的日志。其他一切都只是猜测。
答案 1 :(得分:0)
没有看到任何源代码我非常怀疑你没有很好地收集垃圾。您应该始终在每种方法中清理自己,特别是在一直运行的应用程序中。在完成它们时,总是Dispose()类和其他变量。交换数据后,应关闭与SQL对象的连接。任何打开的文件(IO)也应该关闭,并且应该终止所有被调用的外部线程/进程。
如果没有完成任何这些操作,那么您的程序将快速累积内存使用量,并最终崩溃。
答案 2 :(得分:0)
查看所有非托管资源。如果是da DirectX app,请确保手动释放所有资源。应该处理所有类型为IDisposable的资源。另一个起点是检查你的Serializer,例如你是否有XmlSerializer。
我喜欢Red Gate Memory Profiler,有14天试用版可用: http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/
答案 3 :(得分:0)
因此,在建议使用dotTrace之后,我能够找到内存泄漏。 在这个函数中,由一个计时器调用: private void UpdateEventsScreen() {
int selectedRow = 0;
try
{
if (EventsDataGrid.Rows.Count > 0)
selectedRow = EventsDataGrid.SelectedRows[0].Index;
viewModel = new EventsViewModel();
EventsViewModelSource.DataSource = viewModel;
if (EventsDataGrid.Rows.Count > 0)
{
EventsDataGrid.Rows[selectedRow].Selected = true;
NoteValueLabel.Text = viewModel.Events[selectedRow].Note;
}
}
catch{}
}
我不知道为什么,但行:
viewModel = new EventsViewModel();
导致内存泄漏。我以为垃圾收集器应该处理这个问题。不是吗? 好吧,它没有。
谢谢大家。你帮了很多忙。