每周我都会收到System.OutOfMemoryException:内存不足

Sha*_*ets 0 c# memory-leaks

我有一个程序(C#)与嵌入式控制器(Modbus)通信,接收数据并将其记录到数据库.此外,它还显示用户界面中的数据.

每5或8天程序崩溃(在每台计算机上).

大部分时间没有异常,只有Windows默认消息"'程序名'已停止工作".

每隔一段时间我就会得到'System.OutOfMemoryException:Out of memory.' 例外.我假设程序设计不好,因为内存使用量一直在增加.

一周后它达到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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

mei*_*lke 5

使用dotTrace等内存分析器运行程序5-8天.这将为您提供实际消耗记忆的日志.其他一切都只是猜测.