I have implemented subscription, based on Subscription Data Change() (Monitoring Events). The Programm monitors 80 PLC-Variables.
The issue is, the Memory usage get higher(around 4gb and continuer to be higher..etc) with time when data changes event rises. i don't know what causes of high memory usage,
i tried to reduce app footprint but it didn't work. Possible some data buffering in background...??
This is the code:
Start Monitoring
Code: Select all
// Step 1 --------------------------------------------------
// Create and initialize subscription
m_subscription = new Subscription(m_opcStation.GetSession());
m_subscription.PublishingEnabled = true;
m_subscription.PublishingInterval = 500;
m_subscription.MaxKeepAliveTime = 50000;
m_subscription.Lifetime = m_opcStation.GetSession().SecureChannelLifetime;
// Data change events will be received through Subscription_DataChanged
m_subscription.DataChanged += new DataChangedEventHandler(Subscription_DataChanged);
// Create subscription on server
m_subscription.Create();
// Step 2 --------------------------------------------------
// Prepare variables to monitor as data monitored item
m_monitoredItems = new List<MonitoredItem>();
// Default is monitoring Value attributes
foreach (StationSensorXmlData temp in m_stationDataModel.LineNode)
{
//string sNode = CreateNewNodeIds(m_StationDataModel.dataBlockName, temp.PLCVariable);
NodeId NodeId = new NodeId(temp.PLCVariable, m_opcStation.GetNamespaceIndex());
DataMonitoredItem dataMonitoredItem = new DataMonitoredItem(NodeId, Attributes.Value);
dataMonitoredItem.DiscardOldest = true;
dataMonitoredItem.SamplingInterval = -1;
dataMonitoredItem.QueueSize = 1;
m_monitoredItems.Add(dataMonitoredItem);
}
// Step 3 --------------------------------------------------
// Create monitored items on server
results = m_subscription.CreateMonitoredItems(m_monitoredItems);
Code: Select all
private void Subscription_DataChanged(Subscription subscription, DataChangedEventArgs e)
{
var sensorName = string.Empty;
var ErrorVar = string.Empty;
var StatusCode_errorDebug = string.Empty;
var stationDataSheetTemp = new Tuple<string, string, int, int, string>(string.Empty, string.Empty, 0,0, string.Empty);
try
{
// Check that the subscription has not changed.
if (!Object.ReferenceEquals(m_subscription, subscription))
{
Console.WriteLine("subscription has changed");
return;
}
//Start execution time
var stopwatch = new Stopwatch();
stopwatch.Start();
//m_db.SQLConnectionOpen();
foreach (DataChange change in e.DataChanges)
{
sensorName = change.MonitoredItem.NodeId.Identifier as string;
StatusCode_errorDebug = change.Value.StatusCode.Message;
/* get station data */
bool isPlcVarExist = m_stationDataSheetBuffer.TryGetValue(sensorName, out stationDataSheetTemp);
if (StatusCode.IsGood(change.Value.StatusCode))
{
if (m_stationTyp == 1) /* Master station */
{
ErrorVar = MasterToDb(change, stationDataSheetTemp, sensorName);
}
else if (m_stationTyp == 3) /* Station */
{
/* connect to Master opc */
m_opcMaster.ConnectToOPC(m_opcMasterIp);
if (isPlcVarExist)
{
ErrorVar = StationToDb(change, stationDataSheetTemp, sensorName);
}
else
{
ErrorVar = "Sensor ID not fount!";
}
/* connect to Master opc */
m_opcMaster.Disconnect();
}
else if(m_stationTyp == 2) /* Live bit Master */
{
ErrorVar = LiveBitMaster(change);
}
else
{
ErrorVar = "Station not found!";
}
}
#if DEBUG
Console.WriteLine($"{ErrorVar};{sensorName};{change.Value.WrappedValue}");
#endif
}
stopwatch.Stop(); //Stop execution time
}
catch (Exception ex)
{
m_opcMaster.Disconnect();
#if DEBUG
Console.WriteLine(ex.Message);
#endif
}
}
Best regards,