FieldPoint Family

cancel
Showing results for 
Search instead for 
Did you mean: 

NetworkVariableDataSource.Bindings[VariableName].GetValue() leaks memory

Dear Sir,

We are using Measurement Studio 8.5 for Visual Studio 2005. Our application reads 105 Netwrok Variables in every seconds. It does the work done through NetworkVariableDataSource.Bindings[VariableName].GetValue(). So far we have tested and determined that the GetValue() methods leaks some memory and every seconds the memory consumption by the application goes up. How can we solve this problem? We are using c# lannguage.We need argent solution.

 

Best regards,

Mamunur Rashid,

TechnoVista Limited

0 Kudos
Message 1 of 22
(8,872 Views)

public string readBufferedNetworkVariable()
        {

            // validate variable name and location
            if (string.IsNullOrEmpty(VariableLocation) || string.IsNullOrEmpty(VariableName))
            {
                throw new Exception("Variable Location and or Variable Name can not be Null");
            }
            else
            {
                if (VariableDataSource == null)
                {
                    configureBufferedDataSource();

                }

                try
                {
                    string val;
              
                    val = VariableDataSource.Bindings[VariableName].GetValue().ToString();
              
                    return val;
                }
                catch (ArgumentException)
                {
                    // binding item does not exist
                    configureBufferedVariableBinding();

                    try
                    {
                        string val = VariableDataSource.Bindings[VariableName].GetValue().ToString();                 
                        return val;
                    }
                    catch (InvalidOperationException IOex)
                    {
                        Utility.SaveErrorLog("NINetworkVariableHelper", "readBufferedNetworkVariable", "VariableLocation:" + VariableLocation + " VariableName: " + VariableName + Environment.NewLine + IOex.StackTrace + Environment.NewLine + "Message: " + IOex.Message);                   
                        return "n/a";
                    }

                }
                catch (InvalidOperationException IOex)
                {
                    Utility.SaveErrorLog("NINetworkVariableHelper", "readBufferedNetworkVariable", "VariableLocation:" + VariableLocation + " VariableName: " + VariableName + Environment.NewLine + IOex.StackTrace + Environment.NewLine + "Message: " + IOex.Message);
                     return "n/a";
                }
                catch (Exception ex)
                {
                    // just in case
                    Utility.SaveErrorLog("NINetworkVariableHelper", "readBufferedNetworkVariable", "VariableLocation:" + VariableLocation + " VariableName: " + VariableName + Environment.NewLine + ex.StackTrace + Environment.NewLine + "Message: " + ex.Message);
                     return "n/a";
                }

            }

        }

I have to call the method 105 times in every second.

Message Edited by Mamun on 07-20-2009 10:37 PM
0 Kudos
Message 2 of 22
(8,849 Views)

Mamun,

 

Thanks for the code.  I am going to try and set up a similar system and do some testing to see if I experience the same behavior.  Can you give me some more information about your system and about the memory consumption you are seeing? (RAM, hard drive space, amount of RAM used by the program after 30s, 1 min, etc)

Eric B.
National Instruments
0 Kudos
Message 3 of 22
(8,835 Views)

Dear Eric B.,

 

Some Information about our system:

 

RAM:  2GB

Hard Drive: C: 40GB 😧 214GB E: 210GB

Memory Consumption by 8 browsers simulatiously:

 

10  mins : 126,608 kb

30  mins : 167,196 kb

1 hour    :  236,440 kb

2 hours   : 375,188 kb

2.5 hours : 421,244 kb

...

...

6/7 hours : ~(600,000-700,000) kb and at that point some re-adjustment of memory consumption occur. For example, if it is 650,660 kb in next second it is 645,312 kb.

 

 

Now I would like to explain  the architecture of our application.

 

1) ASPX Page: We have got an .aspx page which includes an ajax timer. The timer refreshes the page in every second where around 105 user controls (5 types of user control) are being refreshed with new value. I will provide you the 'Tick' method which is accounts for  refresing the user controls.

 

2) User Controls:  a) NINetworkVariableButton.ascx : It inherits ASP:Button

                              b) NINetworkVariableLabel.ascx : It inherits ASP:Label

                              c) NINetworkVariableRadioButton.ascx :  It inherits ASP:RadioButtonList

                              d) NINetworkVariableReadOnlyTextBox.ascx :  It inherits ASP:TextBox

                              e) NINetworkVariableTextBox.ascx :  It inherits ASP:TextBox

All user controls  contain properties 1) VariableName 2) VariableLocation and a method 'readData()'. In  'readData()' it uses an instance of class 'NINetworkVariableHelper' and calls the method  'readBufferedNetworkVariable()' of that class.

I will provide 1 of user controls to you.

 

3)  Class 'NINetworkVariableHelper': Reading and Wrinting of data to the network variable are done through this class and the methods are  'readBufferedNetworkVariable()' and 'writeBufferedNetworkVariable()'. I am also going to provide the class.

 

 

0 Kudos
Message 4 of 22
(8,824 Views)

 Dear Eric B.,

 

Following are some code segements

 

Some aspx code: 

 

<td>
<asp:UpdatePanel ID="UpdatePanelDO_KV601A_KV601B" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc3:NIButton ID="DO_KV601A_KV601B" runat="server" VariableName="DO_KV601A-KV601B"
Caption="KV601A-B"></uc3:NIButton>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
</td>
<td>
<asp:UpdatePanel ID="UpdatePanelAO_FC601" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<uc2:NIText ID="AO_FC601" ToolTip="FC601" runat="server" VariableName="FC601"></uc2:NIText>
</ContentTemplate>
<%--<triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"></asp:AsyncPostBackTrigger>
</triggers>--%>
</asp:UpdatePanel>
</td>
<td>
<asp:UpdatePanel ID="UpdatePanel51" runat="server" UpdateMode="conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<uc1:NIReadOnlyText ID="AI_FT601" VariableName="FT601" ToolTip="FT601" runat="server"
Enabled="false"></uc1:NIReadOnlyText>
slpm
</ContentTemplate>
</asp:UpdatePanel>
</td>

 

  Some code of Timer1_Tick of aspx.cs

 

protected void Timer1_Tick(object sender, EventArgs e)
{
refreshCustomControls(Page.Controls[0]);
}
protected void refreshCustomControls(Control control)
{
if (control is NINetworkVariableTextBox)
{
if (!txtActiveControlID.Text.Contains(control.ID))
{
// get the update panel that this user control lives in
// Control updatePnl = Page.FindControl(control.Parent.Parent.ID);
Control updatePnl = control.Parent.Parent;

NINetworkVariableTextBox tb = (NINetworkVariableTextBox)control;

string origValue = tb.Text;

// read the current data from the shared variable
tb.readData();
// tell this update panel to update
((UpdatePanel)updatePnl).Update(); //It is needed to remove yellow back color and update data and here no asychonous postback is used

}
}

else if (control is NINetworkVariableReadOnlyTextBox)
{
if (!txtActiveControlID.Text.Contains(control.ID))
{
FormattingValueOfReadOnlyText(control);
}
}
else if (control is NINetworkVariableButton)
{
if (!txtActiveControlID.Text.Contains(control.ID))
{
((NINetworkVariableButton)control).readData();
}
/* these controls are in asychonous update with timer so dont need to update panel update()*/
}
else if (control is NINetworkVariableRadioButton)
{
//if (control.ID != txtActiveControlID.Text)
if (!txtActiveControlID.Text.Contains(control.ID))
{
((NINetworkVariableRadioButton)control).readData();
}
}
else if (control.ID == "controlgraph")
{
return;
}
else
{
foreach (Control child in control.Controls)
{
refreshCustomControls(child);
}
}
}

 

0 Kudos
Message 5 of 22
(8,822 Views)

User Control NINetworkVariableTextBox.ascx:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NationalInstruments.NetworkVariable.WebForms;
using EndeavorEngineering;
using System.ComponentModel;

public delegate void TextBoxWriteCompletedEventHandler(object sender);

public partial class NINetworkVariableTextBox : System.Web.UI.UserControl
{
NINetworkVariableHelper _NIHelper = new NINetworkVariableHelper();

//string _text = "";
string _width = "50px";
bool _readOnly = false;
bool _enabled = true;
bool _displayAsDouble = true;
string _skinID = "";
string _variableLocation = "";

public event TextBoxWriteCompletedEventHandler TextBoxWriteCompleted;




public string VariableName
{
get { return _NIHelper.VariableName; }
set { _NIHelper.VariableName = value; }
}

public string VariableLocation
{
get { return Session["variableLocation"].ToString(); }
set { Session["variableLocation"] = value; }
//get { return _variableLocation; }
//set { _variableLocation= value; }
}

public string Text
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}

public bool ReadOnly
{
get { return _readOnly; }
set { _readOnly = value; }

}

public string Width
{
get { return _width; }
set { _width = value; }
}

public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}

public bool DisplayAsDouble
{
get { return _displayAsDouble; }
set { _displayAsDouble = value; }
}
public string ToolTip
{
get { return TextBox1.ToolTip; }
set { TextBox1.ToolTip = value; }
}

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

TextBox1.Width = Unit.Parse(Width);
TextBox1.ReadOnly = ReadOnly;
TextBox1.Enabled = Enabled;
TextBox1.ToolTip = VariableName;

if (!ReadOnly)
{
TextBox1.Attributes.Add("onfocus", "SetActiveControl('" + this.ID.ToString() + "',this);");
TextBox1.Attributes.Add("onkeypress", "return newEnterhandler(event)");
}
else
{
TextBox1.BackColor = System.Drawing.Color.Gainsboro;
}

readData();

}
}

public void readData()
{

_NIHelper.VariableDataSource = (NetworkVariableDataSource)Session["ds"];

_NIHelper.VariableLocation = VariableLocation;

string val = _NIHelper.readBufferedNetworkVariable();

if (DisplayAsDouble)
{
if (IsNumeric(val))
{
if (VariableName.Contains("Voltage") || VariableName.Contains("ET") || VariableName.Contains("CurrentDensity"))
{
// three digits of percision for voltage
TextBox1.Text = String.Format("{0:F3}", double.Parse(val));
}
else
{
TextBox1.Text = String.Format("{0:F}", double.Parse(val));
}
}
else
{
TextBox1.Text = val;
}
}
else
{
TextBox1.Text = val;
}
try
{
if ((Session["Role"].ToString() == "Test Engineer" || Session["Role"].ToString() == "Admin")
&& (Utility.GetCurrentState(Session["STSNumber"].ToString()) == Utility.OperationalState.MANUAL))

{
TextBox1.Enabled = true;
}
else
{
TextBox1.Enabled = false;
}

}
catch { }


Session["ds"] = _NIHelper.VariableDataSource;

}

public void writeData(string value)
{
_NIHelper.VariableDataSource = (NetworkVariableDataSource)Session["ds"];

_NIHelper.VariableLocation = VariableLocation;

_NIHelper.writeBufferedNetworkVariable(TextBox1.Text);

//raise event that the write finished
WriteCompleted();

Session["ds"] = _NIHelper.VariableDataSource;

}

protected void _NIHelper_NetworkVariableReadFailed(object sender, EventArgs e)
{

}

protected void _NIHelper_NetworkVariableWriteFailed(object sender, EventArgs e)
{

}

protected void WriteCompleted()
{
TextBoxWriteCompletedEventHandler RaiseTextBoxWriteCompleted = TextBoxWriteCompleted;

if (RaiseTextBoxWriteCompleted != null)
{
RaiseTextBoxWriteCompleted(this);
}
}

protected static bool IsNumeric(object Expression)
{
bool isNum;
double retNum;
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}

protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

HtmlTextWriter hWriter = new HtmlTextWriter(sw);
base.Render(hWriter);

//Image led = _NIHelper.VariableQualityLED;
//led.RenderControl(hWriter);

// *** store to a string
string PageResult = sb.ToString();

writer.Write(PageResult);

}
}


 

 

 

0 Kudos
Message 6 of 22
(8,821 Views)

  Class 'NINetworkVariableHelper'

 

 

public class NINetworkVariableHelper
{
NetworkVariableDataSource _nvDS;
NetworkVariableReader<object> _nvr;
NetworkVariableWriter<object> _nvw;
NetworkVariableData<object> _nvd;



string _variableName;
string _variableLocation;
string _variableQuality;
string _variableQualityText;

public event NetworkVariableWriteFailedEventHandler NetworkVariableWriteFailed;
public event NetworkVariableReadFailedEventHandler NetworkVariableReadFailed;

public NINetworkVariableHelper()
{

}

#region Properties

public string VariableName
{
get
{
return _variableName.Substring(_variableName.IndexOf("_") + 1);
}
set
{
_variableName = value;
}
}

public string VariableLocation
{
get
{
return _variableLocation;
}
set
{
_variableLocation = value;
}
}
public NetworkVariableDataSource VariableDataSource
{
get
{
return _nvDS;
}
set
{
_nvDS = value;
}
}


#endregion

public string readBufferedNetworkVariable()
{

// validate variable name and location
if (string.IsNullOrEmpty(VariableLocation) || string.IsNullOrEmpty(VariableName))
{
throw new Exception("Variable Location and or Variable Name can not be Null");
}
else
{
if (VariableDataSource == null)
{
configureBufferedDataSource();

}

try
{
string val;

val = VariableDataSource.Bindings[VariableName].GetValue().ToString();

return val;
}
catch (ArgumentException)
{
// binding item does not exist
configureBufferedVariableBinding();

try
{
string val = VariableDataSource.Bindings[VariableName].GetValue().ToString();
return val;
}
catch (InvalidOperationException IOex)
{
Utility.SaveErrorLog("NINetworkVariableHelper", "readBufferedNetworkVariable", "VariableLocation:" + VariableLocation + " VariableName: " + VariableName + Environment.NewLine + IOex.StackTrace + Environment.NewLine + "Message: " + IOex.Message);
return "n/a";
}

}
catch (InvalidOperationException IOex)
{
Utility.SaveErrorLog("NINetworkVariableHelper", "readBufferedNetworkVariable", "VariableLocation:" + VariableLocation + " VariableName: " + VariableName + Environment.NewLine + IOex.StackTrace + Environment.NewLine + "Message: " + IOex.Message);
return "n/a";
}
catch (Exception ex)
{
// just in case
Utility.SaveErrorLog("NINetworkVariableHelper", "readBufferedNetworkVariable", "VariableLocation:" + VariableLocation + " VariableName: " + VariableName + Environment.NewLine + ex.StackTrace + Environment.NewLine + "Message: " + ex.Message);
return "n/a";
}

}

}
}

 

 
0 Kudos
Message 7 of 22
(8,820 Views)

Hi Mamun,

 

Thank you again for the code, it was very helpful in trying to replicate the issue to see if I saw the same behavior.  I was able to create an ASPX page bound to 10 variables that called getValue 10 times per second and I did see some memory leakage.  I will be filing a bug report on this to the developers and we are looking to see if there is a workaround for your situation aside from just closing and reopening the browser periodically to release the memory.

Eric B.
National Instruments
0 Kudos
Message 8 of 22
(8,797 Views)

Hi Eric B.,

Any update?

 

Mamun

Software Engineer

TechnoVista Limited

0 Kudos
Message 9 of 22
(8,644 Views)

Mamun,

 

I filed a bug report with the developers shortly after my last post.  The bug ID for this report is 183378.  You can use this bug ID to look at the released list of fixed bugs every time Measurement Studio is released to see if the bug has been fixed.  The report was assigned to a developer and they are looking into a resolution, but I don't know much more than that.

Eric B.
National Instruments
0 Kudos
Message 10 of 22
(8,626 Views)