Note that there are some explanatory texts on larger screens.

plurals
  1. POEvery week I get System.OutOfMemoryException: Out of memory
    text
    copied!<p>I have a program (C#) that communicate with embedded controller (Modbus), receiving data and log it to a data base. also, it shows the data in a user interface.</p> <p>Every 5 or 8 days the program crashes (on every computer).</p> <p>most of the time with no Exception and only the Windows default message "'program name' has stop working".</p> <p>every once and a while I get the 'System.OutOfMemoryException: Out of memory.' Exception. The program, I'm assume is not designed well because the memory usage is increasing all the time.</p> <p>After a week it reaches 800Mb.</p> <p>Is this the problem?</p> <p>I have tried reset the program every day. It didn't solve the problem.</p> <p>This is the source:</p> <pre><code>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&lt;ReactorEvent&gt; getEvents(ReactorConfig reactorData) { try { ushort registerNum; List&lt;ReactorEvent&gt; eventsList = new List&lt;ReactorEvent&gt;(); // List that will hold all of the data (all the registers) List&lt;ushort&gt; registerValues; // Get's the data from the modbus for (int i = 0; i &lt; ReactorEventConfigManager.ReactorEvents.Count; i++) { registerNum = Convert.ToUInt16(ReactorEventConfigManager.ReactorEvents[i].ModBusRegisterNumber); // Convert the register numb to Unsigned short registerValues = new List&lt;ushort&gt;(); // Initlize the list try { mb.SendFc3(reactorData.ModBusAddress, registerNum, 1, registerValues); } catch { throw new NoConnectionException(); i = ReactorEventConfigManager.ReactorEvents.Count; } if (registerValues.Count &gt; 0 &amp;&amp; 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; } } /// &lt;summary&gt; /// Reads data from the controller /// &lt;/summary&gt; /// &lt;param name="reactorNumber"&gt;the reactor's ndbuumber&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public Dictionary&lt;Register, string&gt; ReadData(ReactorConfig reactorData, List&lt;short&gt; unitedRegisters) { try { //Register tempRegister = new Register(); Dictionary&lt;Register, string&gt; returnData = new Dictionary&lt;Register, string&gt;(); string strRegisterValue; int registerLocation = 0; List&lt;ushort&gt; registerValues = new List&lt;ushort&gt;(); // List that will hold all of the data (all the registers) List&lt;ushort&gt; registerValuesNew = new List&lt;ushort&gt;(); List&lt;bool&gt; coilsValues = new List&lt;bool&gt;(); ushort startRegister, endRegister, midRegister; startRegister = endRegister = midRegister = 0; if (getStartAndEndReg(ref startRegister, ref endRegister, LoggerConfigManager.LoggerConfiguration.RegistersToLog) == 1) { try { if (endRegister &gt;= 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) &amp;&amp; (reactorNum == reactorData.Number)) { shutdownLamp(reactorData, systemOnOffState); systemOnOffStatus = false; } for (int t = 0; t &lt;= 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) &amp;&amp; (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 &lt;= 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 &amp;&amp; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload