Brian,
I used a memory profiler and everytime the app has to create a new
Stringbuilder the old one remains in the memory leaving the size of 2Mb
unchanged. I called the GC, but still nothing happens. I am sure that the
declared variable is only within the function. I even tried to set the
variable to null, but then the app creates an error. Below you can find the
code that I am using. It might not be the best ever written code. It is just
created to have a quick solution to generate time for a much better one.
Maybe someone of you can help me to figure out what is going wrong.
TIA,
Arjan.
private static string jmsSBuilder(string data, string idTag, int iSeqNr)
{
DateTime setTime = DateTime.Now;
StringBuilder buffer = new StringBuilder();
string sTime = setTime.Year.ToString() + "-" + setTime.Month.ToString() +
"-" + setTime.Day.ToString();
sTime += "T" + setTime.Hour.ToString() + ":" + setTime.Minute.ToString() +
":" + setTime.Second.ToString();
buffer.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buffer.Append("<informa xmlns=\"http://www.informa.com/inf.xsd\">");
buffer.Append("<header>");
buffer.Append("<version>1.0</version>");
buffer.Append("<messageid>");
buffer.Append(iSeqNr.ToString());
buffer.Append("codamid");
buffer.Append(idTag);
buffer.Append("</messageid>");
buffer.Append("<correlationid>");
buffer.Append(iSeqNr.ToString());
buffer.Append("codacid");
buffer.Append(idTag);
buffer.Append("</correlationid>");
buffer.Append("<timestamp>");
buffer.Append(sTime);
buffer.Append("</timestamp>");
buffer.Append("<messagetype>");
buffer.Append(esbMessageType);
buffer.Append("</messagetype>");
buffer.Append("<environment>");
buffer.Append(esbEnvironment);
buffer.Append("</environment>");
buffer.Append("<topic>");
buffer.Append(esbTopic);
buffer.Append("</topic>");
buffer.Append("<dbaction>");
buffer.Append(esbDBAction);
buffer.Append("</dbaction>");
buffer.Append("<informa_company>");
buffer.Append(esbCompany);
buffer.Append("</informa_company>");
buffer.Append("<procedure>");
buffer.Append(esbProcedureName);
buffer.Append("</procedure>");
buffer.Append("</header>");
buffer.Append("<body>");
buffer.Append(data);
buffer.Append("</body>");
buffer.Append("</informa>");
string sMain = buffer.ToString();
buffer.Remove(0, buffer.Length);
return sMain;
}
private static void DataProcessing(string sConn, string sSQL)
{
string sGuid = Guid.NewGuid().ToString();
int MaxLength = esbMaxMessageSize * 1024;
int SeqNr = 0;
string jmsMessage = string.Empty;
int hdLength = jmsSBuilder(string.Empty, sGuid, SeqNr).Length;
bool audit = false;
StringBuilder info = new StringBuilder();
StringBuilder rij = new StringBuilder();
SqlConnection cn = new SqlConnection(sConn);
SqlCommand rs = new SqlCommand();
SqlDataReader dr = null;
try
{
cn.Open();
rs.Connection = cn;
rs.CommandText = sSQL;
dr = rs.ExecuteReader();
while (dr.Read())
{
rij.Append("<row>");
for (int i = 0; i < dr.FieldCount; i++)
{
rij.Append("<");
rij.Append(dr.GetName(i));
rij.Append(">");
rij.Append(Convert.ToString(dr.GetValue(i)));
rij.Append("</");
rij.Append(dr.GetName(i));
rij.Append(">");
}
rij.Append("</row>");
if ((info.Length + hdLength + rij.Length) < MaxLength)
{
info.Append(rij.ToString());
}
else
{
jmsMessage = jmsSBuilder(info.ToString(), sGuid, SeqNr);
audit = SonicMQ(jmsMessage);
SendAudit(audit);
info.Remove(0, info.Length);
info.Append(rij.ToString());
SeqNr++;
}
rij.Remove(0, rij.Length);
}
if (info.Length != 0)
{
jmsMessage = jmsSBuilder(info.ToString(), sGuid, SeqNr);
audit = SonicMQ(jmsMessage);
SendAudit(audit);
info.Remove(0, info.Length);
}
}
catch (Exception err)
{
SendEventLog("DataProcessing" + ": " + err.Message,
EventLogEntryType.Error);
}
finally
{
if (!dr.IsClosed)
dr.Close();
if (cn != null)
cn.Close();
}
}
"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Arjan,
The only thing you need to do is stop referencing the object and it
will eventually collected by the GC. What makes you think the memory
isn't being released?
Brian
Arjan wrote:
>Hello,
I have been looking for a couple of days now, but I can't find anything
about how to deal with StringBuilder and releasing the memory used by it.
When I use stringbuilder, the memory doesn't get released (or actually it
looks like that) and thus the used memory keeps growing. Is there a
solution
to this?
Regards,
Arjan.